Reputation: 3834
I have a multilanguage site and I would like PHP to automatically set the language depending on the location from where you enter the site.
I tried a couple of ways.
localeconv()
is not returning local language at all,nl_langinfo()
was also not helpful at all,mb_language()
returns not the language I was looking for,$_SERVER['HTTP_ACCEPT_LANGUAGE']
this returned me a couple of languages instead of just one.setlocale(LC_ALL, 0)
returned C
for some reason.But I failed to get the correct info every time.
I guess that setlocale(LC_ALL, 0)
is the best solution, but I don't know what the returning C
means and I don't know what to expect from different languages.
I looked for a solution on many different sites (including SO) and found the solutions I mentioned earlier. Unfortuately none of them did what I was looking for.
Upvotes: 0
Views: 622
Reputation: 2646
Short answer: Language and location are very different things. You shouldn't set the language based on the location.
Why?
Many countries have multiple languages. Additionally, if you are English and you log onto your favourite website while you are on holiday in Japan, you don't want to see it in Japanese.
As Johannes mentioned, better to use the browser's language ($_SERVER['HTTP_ACCEPT_LANGUAGE']
) if you want to make that decision automatically.
Upvotes: 0
Reputation: 930
Another approach without substr: locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE'])
Upvotes: 1
Reputation: 67738
I use $language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
to get the first (= preferred) entry of the language array, reduced to 2 characters, for example "en" or "de"
Upvotes: 2