Open rbjarnason opened 13 years ago
We don't have it in tr8n yet. But that would be a nice addition. There are few ways to determine the default language of the user. As you mentioned, one of them would be to use the ip, map it to the country and map it to the language. Unfortunately, country to language level mapping may not be enough, as there may be many different languages spoken in different regions (or even in the same region) of a country. A more reliable way would be to trust user browser default language.
common_methods.rb which gets included into your application_controller has a method called:
tr8n_user_preffered_locale
this method uses
tr8n_browser_accepted_locales
to determine the first available language enabled in your site's tr8n, that is accepted (or specified) in the user's browser.
The user language preferences are usually passed by the HTTP_ACCEPT_LANGUAGE header and it is usually a list of languages in user's priority - most desired first.
So i would recommend to use that approach - If your user has not specifically chosen a language, use the tr8n_user_preffered_locale method to get the default locale for the user.
Hope that helps.
The problem with browser default language is the common usage of English browsers in countries around the world, in the Nordic countries for example most people use English versions of their browsers but expect sites like Facebook to be localized in their language. I think this is quite common in different parts of Asia as well. For our Your Priorities project its critical to try to give the best default language as possible and we believe the IP Country lookup is better than browser language settings.
I've now implemented a basic version of this feature that uses iso 3166 and information from the CIA Factbook. This process results in around 200 correct country to language translations.
This feature is included in this commit: https://github.com/rbjarnason/tr8n/commit/42b9c017ac4570910f13ef70445b217b472e3785
Here is the data from the CIA and we are using the 3166 data from the iso website. https://www.cia.gov/library/publications/the-world-factbook/fields/2098.html
One soft dependency on the IsoCountry class are the country flags, free icons for non-commercial but they suggest $40 donation for commercial use. But not really sure if this should be a part of tr8n... You can see this here: https://github.com/rbjarnason/open-active-democracy/tree/master/public/images/flags
Is there a map file available anywhere that maps country codes to the language codes used in tr8n? To be used to default to the "right" language based on geoip return codes.