Forums

Easiest way to force language switch? (7 posts)

  1. adamtal
    Member
    Posted 5 months ago #

    I want to write a plugin that changes the admin interface to a certain language once activated.

    I want to allow people who have used a one-click installation of WordPress (through hosting services like Dreamhost) to switch languages without using FTP.

    I want the plugin to use a remote host where the language file (.mo) are hosted, so the users won't have to download/upload them on their own, and so the plugin is not dependand on existing language files.

    I'm assuming the easiest way would be somehow overriding the settings of wp-config.php, but I'm assuming this is not possible via plugins.

    What would be the easiest way to achieve this?

  2. Chouby
    Member
    Posted 5 months ago #

    Did you look at WP Native Dashboard?

  3. adamtal
    Member
    Posted 5 months ago #

    Thanks Chouby,

    The Native Dashboard plugin relies on downloading the language files to the user's host, and calling them locally. I'd like my plugin to be a one-click ("activate") solution, and want it to rely on remote language files, which I'm not sure if possible (or how to achieve).

    I scanned the plugin's code for hints about language forcing but couldn't find a solution for my problem (for example a reference to the language path), but maybe I missed something. Any help will be highly appreciated!

  4. Chouby
    Member
    Posted 5 months ago #

    To force the language can be done by filtering 'get_locale'. Here I read it in a user meta that can be created / updated based on a form somewhere you want. The code has to be placed in a class.

    add_filter('locale', array(&$this, 'get_locale'));
    
    // returns the locale based on user preference
    function get_locale($locale) {
    // get_current_user_id uses wp_get_current_user which may not be available the first time(s) get_locale is called
    	if (function_exists('wp_get_current_user'))
    		$loc = get_user_meta(get_current_user_id(), 'user_lang', 'true');
    	return isset($loc) && $loc ? $loc : $locale;
    }

    I would fear that if your plugin is widely used, using remote languages files may result in high server disk load. Whereas keeping it locally with spread the disk load on all users servers.

  5. adamtal
    Member
    Posted 5 months ago #

    Hey Chouby,

    Thanks so much for the help. Not sure I really understand how your code allows me to use a remote path (I'll probably be able to handle the server disk load - not worried about that).

  6. Chouby
    Member
    Posted 5 months ago #

    The code above won't help you to use a remote path. I will only help you to force the language.

    To use a remote path, I would try to override the function 'load_textdomain' (that you can find in wp-includes/l10n.php) using the filter 'override_load_textdomain'. By returning true, you can prevent WordPress using the local files and do your own treatment in your function.

  7. adamtal
    Member
    Posted 3 months ago #

    Chouby - thanks so much for the help!

    Any chance you can give me a quick idea of how to use it? (my php is rusty... everything works so well lately I hardly ever need it :))

Reply

You must log in to post.

About this Topic