0

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.

  1. localeconv() is not returning local language at all,
  2. nl_langinfo() was also not helpful at all,
  3. mb_language() returns not the language I was looking for,
  4. $_SERVER['HTTP_ACCEPT_LANGUAGE'] this returned me a couple of languages instead of just one.
  5. 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.

1
  • 1
    If somoene votes down I would appreciate an explenation. Perhaps I need to edit my question somehow? Commented Feb 7, 2019 at 15:37

3 Answers 3

2

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"

Sign up to request clarification or add additional context in comments.

3 Comments

I fought about that, but I am in the UK and yet first language that is displayed is "pl".
well, that must either be the language you set as the preferred language in your browser, or you use Polish as the language in your OS.
Indeed. Though the OS language is English, probably the browsers preffered language is Polish. I checked with different browser and it showed me "en'. Thanks :)
1

Another approach without substr: locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE'])

7 Comments

I think that would be a better solution if only it worked. Fatal error: Uncaught Error: Call to undefined function locale_accept_from_http() in ...
@Krzysztof Can you check for the intl extension in your php.ini?
Did you restart your server? And is there an entry in your phpinfo?
I did restart, but don't see it in phpinfo
You have to search for intl in your phpinfo.
|
0

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.

2 Comments

That would make sense if I didn't store the language after it is changed by a user in the site and I don't check for language anymore. So I think taht in this case depending on location is not a bad solution.
Ok but which language are you going to use for Belgium?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.