1

I am coming from a multi-linguo country, and viewers may speak different language.

I am writing a theme and I noticed that the language of my theme only changed based on the setting of my admin panel, which mean only logged-in user or admin can do so.

What if I wanted to do it automatically by detecting user browser preference? I am using Jquery to achieve some of this function but thinking it not the right way.

What I was doing is use jquery to set initial language by browser locale, and allow user to change it with press of a button, then save the setting to cookie.

I also tried using PHP to detect browser language, but what is the API to tell wordpress to show the correct language (e.g. screen-reader-text)? or allow a site visitor to change the language on-the-fly?

is there a "cleaner" way of doing it in wordpress using i18n on server side(while detecting browser setup and then letting viewers to change the language preference)?

  • 1
    you can use a plugin like polylang which install several languages in wordpress and detect the language the first time an user comes : wordpress.org/plugins/polylang – mmm Jul 13 '18 at 10:44
  • Can I make manual translations to the menu items with this plugin? cuz I don't want some auto translate that might make weird translations. – adrian li Jul 13 '18 at 12:45
  • Pretty amazing how little attention this question has gotten - seems like a very common need. – Isaac Lubow Oct 9 at 19:16
0

You can get the browser language from the PHP global $_SERVER['HTTP_ACCEPT_LANGUAGE'] )

  • yes i tried that too, but what is the API to set the wordpress's Locale? After the php gets the language info from the client side, how does it tell wordpress to display desired corresponding language? And could it be set manually by visitor who browser the page? – adrian li Jul 13 '18 at 11:42
  • Hmm.. that I don't know, but wp-config.php has the line define('WPLANG', ''); - have you tried that? wp-config.php is just a .php script, so there's no reason why you can't read the global and modify that line, or read a cookie and use that to set it (thus allowing your visitor to change the language). – Peter HvD Jul 13 '18 at 11:54
  • that seemed to hv to do with admin setup but not for regular site visitors, let me try... – adrian li Jul 13 '18 at 12:43
  • Found reference in the Codex for a locale filter. It doesn't give much detail, but there is an example: codex.wordpress.org/Plugin_API/Filter_Reference/locale – Peter HvD Jul 13 '18 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.