Welcome Guest, Not a member yet? Register   Sign In
Mapping Country Codes to user agent languages
#1

[eluser]Xeoncross[/eluser]
The user_agent class fetches the languages that the user_agent states it likes for us. However, I have a list of countries and they only have ISO2 and ISO 3 codes that don't match up with the lang the user agent suggests. So how should I deal with this?

"en" and "en-us" is what the browser sends - but country codes are like "us" and "usa".


Code:
//Fetch all countries
$query = $this->db->get('countries');

//Get the languages this user likes
$languages = $this->agent->languages();
    
    
foreach($query->result() as $country) {
    if(in_array($country->iso2, $languages)) {
         print 'found';
    }
}
#2

[eluser]TheFuzzy0ne[/eluser]
Please excuse my ignorance, but I don't understand why you need to match a country code. en could be England, Scotland or Wales also.

Please could you explain exactly what you are trying to acheive? I can understand why you might want to find the user's spoken language, but I can't understand why you need to know the country. If you need to know where a user is based, and perhaps you can convert the country code into a language code instead, there's a good tutorial here: http://v3.thewatchmakerproject.com/journ...-and-mysql
#3

[eluser]Xeoncross[/eluser]
Thanks for the link to that article.

I want to know what country the user is in for the purpose of helping users find other people in their country. I don't have any translations yet so I don't really need to know what language they speak. I just want to pre-select the correct country on the registration form.

Later however, I will want another language option so that I can use translations.
#4

[eluser]TheFuzzy0ne[/eluser]
May I suggest that you simply ask your users where they are from and perhaps what languages they can speak? This would give them freedom and mean that there's less to go wrong because your system will break if someone is coming via a proxy of some sort. If an Indian guy wants to be Chinese, who are we to stop him? Besides, just because someone comes from a specific country doesn't mean they want to see pages in the native language for the country they are in. The may simply be on holiday, or working abroad temporarily.
#5

[eluser]Xeoncross[/eluser]
Yes, I show a list of all 150+ countries but I don't want them to have to scroll though it if I can help auto-select the one they are from.
#6

[eluser]TheFuzzy0ne[/eluser]
Ah, OK. I get you. It's a neat idea if you can get it to work, but personally, I think I would use <optgroups> in my select box, and break the countries up into continents or something to that effect. There are examples of this all over the Web on various sign up forms. I have had to select my country from a select box many times, and I had no problem locating my country name.

Sorry I couldn't help you out more.
#7

[eluser]darkhouse[/eluser]
Another idea would be to use [url="http://www.ip2nation.com/"]ip2nation[/url] and select their country based on their IP. If you're giving the user the option to change it anyways, this should work fine. Obviously you can't rely on it to be 100% accurate because people can spoof their IP, but it should server the purpose you intend it for.

When I'm filling out forms, I've 'programmed' myself to press C three times to get Canada when I'm on the country field, so it's usually very easy for me to find my country. It doesn't always work, some websites don't have the full country listing, or they're not in the right order, but I'd say 9 times out of 10 it works.

Just a note on selecting languages based on country, you wouldn't be able to think that just because you know what language the user is using that you know where they're from. I'm from Canada, near Toronto and there are tons of people here that speak english, french, chinese, japanese, korean, hindi, spanish, german, italian, hungarian and I'm sure there are plenty other languages people speak in my general area. These are just the ones I know about, people I've gone to school with, neighbours, friends of family, etc. Point is, it would be terrible to assume where a person is from based on their preferred language.




Theme © iAndrew 2016 - Forum software by © MyBB