Change “en-US” to “en”
-
29-09-2020 - |
質問
Hey I am trying to edit the WP theme files, and I am using bloginfo('language')
to get the language of the page.
However, it returns me "en-US", which does not work while redirect the page. The site I am working on only accept "/?lang=en".
I have tried to use if statement to specify the solution under different situations, but it failed, it seems the theme used a complex loop method, a slightly change will make the functions does not work.
So could I ask if there is an alternative way to transfer "en-US" to "en", or get "en" directly from the browser?
Thank you.
解決
If you want to get the language of the browser do so:
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
If you want to redirect to ?lang=en
:
header("location: {$_SERVER['REQUEST_URI']}?lang={$lang}");
You should read about HTTP_ACCEPT_LANGAUGE.
There are many ways you can dance with languages but you should understand some key points.
- Chose the default language of your website (French in your case)
- The user may be French but his OS & Browser can be in English because he wants to learn the language of trade.
- Create a cookie that stores the selected (or default) language so that you know what content to display.
$_COOKIE["wpse_166976_lang"] = "fr"
or$_COOKIE["wpse_166976_lang"] = $land
where$lang = get_query_var("lang")
To get the language with WPML plugin, you could use ICL_LANGUAGE_CODE
to get the language of the page