Support » Fixing WordPress » Set Translation in functions.php Theme file

  • Hello everybody,

    I’m not sure if I am in the good section of the forum, but here’s the thing: I am developping my own WP theme for a website that needs to be available in 2 languages.

    I have read several pages (in the Handbook for Devs, in this forum, in the web…), but I haven’t found what I am looking for. I started with what is written in here:

    https://developer.wordpress.org/themes/functionality/internationalization/

    But, let’s be honest, to set his favourite language, the users of my website won’t be changing the wp-config.php by themselves…

    So far, I have been thinking of 2 ways of doing it. Let’s call them “plan A” and “plan B”.

    Plan A consist of:
    1) the user sets his favourite language thanks to a <select>
    2) the page is directly displayed with the good translations (with a javascript function), without reloading
    3) the language preference is set in a cookie so when the user comes later, his favourite language is automatically set

    This is the best UX I can think of. Yet I haven’t found any documentation about that. So I’ve come to a plan B :

    1) the user sets his favourite language thanks to a <select>
    2) in a JS file, the preference is set in a cookie, then the page reloads automatically
    3) Thanks to the cookie, a PHP function (in the functions.php file of the theme) set the favourite language

    In the best case, I’ll prefer the plan A, but the plan B is totally acceptable. What I need is, for both cases, how to “say” to WordPress “The language to be show is…”, or in another term, which JS (or PHP) function should I use (in my JS file, or functions.php respectively…)?

    Thank you in advance for your help.. 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • Topher

    (@topher1kenobe)

    This post should help: https://translatepress.com/change-wordpress-language/

    I don’t think you’ll need to build anything.

    Thread Starter sam0kc

    (@sam0kc)

    It actually doesn’t help, because as said in the first post, the visitors of the website won’t go in the administration panel to set their preferred language

    Thread Starter sam0kc

    (@sam0kc)

    To precise something: the translations are handled with .po files, in the /language folder of the homemade theme…

    Thread Starter sam0kc

    (@sam0kc)

    Okay, I found a solution. It’s actually my “plan B”.

    With a <select name=”langageSelect”> in the PHP file, here is the content of the JS file:

    document.querySelector('select[name=languageSelect]').onchange = setCookieAndReload;
    function setCookieAndReload() {
    	MsSepPreferredLanguage = document.querySelector('select[name=languageSelect]').value;
    	document.cookie = "preferredLanguage="+MsSepPreferredLanguage;
    	window.location.reload(true);
    }

    This is gonna set a cookie to remind the language preferences of the user..

    … and added in the functions.php file:

    function wpse_52419_change_language( $locale )
    {
        return $_COOKIE['preferredLanguage'];
    }
    add_filter( 'locale', 'wpse_52419_change_language' );
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.