Of course, it is good to be polite and this includes recognizing that English may not be the other person's first language. However, beyond that, I don't think that there is any one size fits all answer. A little research can help. Here are some of my experiences in attempting this.
In Denmark, especially Copenhagen, the standard of English is so high that I feel that people are even slightly offended when I ask "Do you speak English?" whether I do it in English or bad Danish. So, I have stopped doing this unless there is some reason to doubt the other person's ability. I had an amusing experience when checking in at Copenhagen airport once: the agent said: "Forstå du dansk?", I replied "Nej". She gave a little puzzled smile and switched to perfect English.
Netherlands, especially Amsterdam, is similar but with the extra twist that it is quite likely that the other person does not speak Dutch either. Once I attempted Dutch in a restaurant and the waitress replied: "sorry, I don't speak Dutch, I am Lithuanian".
The next factor that I look for is whether it is common for foreigners to learn the local language. Examples are: France, Germany, Spain, and Italy. In these cases, people are more used to hearing their own language spoken badly and will probably at least realise that you are trying to speak it. So, I attempt to learn a few basic phrases including "do you speak English?".
Next, be more careful when there are multiple local languages. Belgium is complex and you should look for clues as to whether you are in a French speaking area before attempting French. This also applies to Spain, if you are in a Catalan speaking area then recognizing that is appreciated, see next case.
You are in country or region where few foreigners attempt to learn the language. This can be challenging since, unless you have significant linguistic skills, the other person might not realize that you are attempting to speak their language. Once in Barcelona, I tried to learn a few basic Catalan phrases but I failed miserably. I don't think that people realized that I was even trying to speak their language. I know some Spanish (or rather Castilian) but I get the feeling that assuming that someone speaks Castilian is almost as bad as assuming that they speak English. I finally settled on saying in Castilian: "I am sorry, I don't speak Catalan, do you speak Castilian?". The answer was always yes but I got a much better reception than just assuming it. Note my use of Castilian for what English speakers commonly call Spanish. Recognizing that Castilian is not the only language in Spain also helps.
Portugal is an intermediate case. I have some success with simple Portuguese but also some Catalan like failures.
In these cases, you may as well just stick to English but be as polite as you can be. If you know some alternative language then it might help e.g. many Portuguese people know French. Of course, don't assume that but try it if you can.
All of those examples are European but the same applies elsewhere.
Quite a few foreigners attempt to learn Thai. Not many get very far but it is common enough that they will probably realize that you are trying and they may recognize your badly spoken simple phrases.
The same in Malaysia though you have the Belgium type problem that the native language of many is Chinese.
In Cambodia, few foreigners attempt to learn Khmer so unless you are very good at languages, they might not realize that you are trying to speak it.
China is a nice case. Enough foreigners attempt Mandarin that they might realize that you are trying. You can get an amazing positive reaction with just a few simple phrases. Not so for Cantonese though and there are yet other forms of Chinese. Attempting Mandarin in Hong Kong is rather like Spanish in Barcelona.
Finally the Philippines is another case. They usually recognize that you are trying to speak Tagalog but they are so unused to hearing it spoken badly that they are liable to collapse in laughter.