0

From the following JavaScript:

<script>
var language = navigator.language || navigator.userLanguage;
</script>

...is then possible to echo text related visitor's browser language?

For example: You are watching MyMovieTitle [<?php echo navigator.language ?>]

The javascript should provide me the full language word. Example: 'English'; 'German'; 'French' and so on...

  • No. PHP gets parsed on the server, way before it even reaches the users browser (where the javascript is executed) and you can't echo Javascript variables with PHP. – M. Eriksson Jun 04 '17 at 15:50
  • You can create an array after detecting user language and print message according to array value `alert ("The language is: " + language);` – Rtra Jun 04 '17 at 15:52
  • If you really need the language in PHP, you can check if the browser sends the language as a header. Here's ha post about it: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php – M. Eriksson Jun 04 '17 at 15:52
  • Can't you just output `language` in the browser via JS? Whys the PHP needed? – chris85 Jun 04 '17 at 15:54
  • @MagnusEriksson - In theory you're partially correct. But, the server is receiving a request from the browser, so, it can receive locale information along with that. – Umashankar Das Jun 04 '17 at 16:06
  • @Rtra language.indexOf('fr') = 'Français'; else if language.indexOf('de') = 'Deutsch' and so on... Would be real long. That's why I want the system to detect and print itself. So that in case of Czech Republic; Sweden, Russia, and so on, it will do it alone, without me pre-inserting the whole country list and words... – Chicö Latinö Jun 04 '17 at 16:06
  • @ChicöLatinö - Do check my reply. Hope that helps. – Umashankar Das Jun 04 '17 at 16:07
  • Possible duplicate of [how to get the browser language using javascript](https://stackoverflow.com/questions/8199760/how-to-get-the-browser-language-using-javascript) – Alexander Jun 04 '17 at 17:15
  • Nope it isn't. I'm not asking how to get browser language. But how to print entire language word. – Chicö Latinö Jun 04 '17 at 20:17
  • @UmashankarDas - Wasn't that exactly what I said in my second post? My first post was about using js-variables in PHP, which you can't do. – M. Eriksson Jun 05 '17 at 05:13

1 Answers1

0

You could just print the javascript variable

navigator.language || navigator.userLanguage;

But, since, you want,the PHP version use this variable.

$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale;
$language = locale_get_display_language($locale);
echo $language; // Non-Ugly version :) . Got anglais for en-US

Please check above and see.

Umashankar Das
  • 601
  • 4
  • 12
  • Ugly Results. Wow. Anyhow, check the modified answer. FYI Stackoverflow is not built to give you tailormade data. It will give you the path to go forward. Incidentally, the result above will be fr-FR since, q is 0.8 which sounds highest. Surely you can learn to parse that. – Umashankar Das Jun 04 '17 at 16:54
  • Now reports: "Call to undefined function locale_accept_from_http() ..." – Chicö Latinö Jun 04 '17 at 17:12
  • which version of php are you using ? – Umashankar Das Jun 04 '17 at 17:14
  • You need to read some PHP. You can get php version by doing this. echo phpinfo(); If you version is < 5.3.x then you need to parse the previous output from the accept header // The Ugly version :) – Umashankar Das Jun 04 '17 at 17:23
  • PHP Version 5.4.45 – Chicö Latinö Jun 04 '17 at 18:05
  • If useful, I'm using wordpress. And I can tell you if I just try to add in my header.php file, the whole site cant be open, due to a syntax error... – Chicö Latinö Jun 04 '17 at 20:16
  • 1
    @ChicöLatinö - Make sure that you have installed the PHP extension `php5-intl`, which is required for the above functions. – M. Eriksson Jun 05 '17 at 05:11
  • Is this stuff based on my website server side, or involves my browser(s) in use? I ask coz in first case it would be a solution, making visitors see what I plan to; viceversa in second case I would be able to display correct word, but all visitors without php5-intl installed wouldnt. – Chicö Latinö Jun 05 '17 at 08:36
  • Now worked. Thanks a lot both. Especially Magnus Eriksson who gave me the ultimate input. – Chicö Latinö Jun 05 '17 at 09:46