How to change the language for the front-end only?

I have installed a wordpress theme, i can see the .mo and .po /public_html/wp-content/themes/themename/lang, the language I want to use is available.

Could you please advise on how to use a specific language? I don’t want to change the admin language just the theme language.

Related posts

7 comments

  1. Well, you actually do not need anything special to have two different languages in the back end and in the front end. I just tested it again on WP 5.1

    1. go to the settings and set the Site Language to the front end laguage, for example – German, then save.

    2. go to Users and then click edit on your administrator account. There you can set Language to English and save. That’s it. Your front end will use German as base and the back end will be in English.

  2. If anyone is still looking for that, here is what you should do since version 4.7

    function wp_noshor_redefine_locale($locale) {
        if( is_admin() ):
            switch_to_locale('en_US');
        endif;
    }
    add_filter('init','wp_noshor_redefine_locale');
    

    This forces the dashboard to load in English, then you can go to settings, set the language you desire.

  3. You can filter the string for locale, it holds the current language. And you can exclude the admin from that filter:

    is_admin() or add_filter( 'locale', function() {
        return 'ar';
    });
    
  4. Here is the complete and a better solution to only change theme (front-end) or plugin language. It even works for changing even a specific page/post etc. language as well.

    TLDR; There is a filter called locale, which determines which language to load. You can change to your specific language based on the conditions that you prefer.

    Changing only Theme language, or based on other conditions, without touching the admin dashboard language.

    add_filter('locale', function($locale) {
    
        if( is_admin() ) {
            return $locale; // Leave default language if it is admin side.
        }
    
        // You can load language based on any conditional tags here, like, is_page(), is_singular(), is_archive() etc.
        // I want to change the language for all front end in this particular example.
    
        $locale = 'en_US'; // Change to your required language. The language must be installed and loaded.
    
        return $locale; // Return language to be used.
    
    });
    

    Before you change the language, you may need to load that language first.

    In your theme or child theme, load the custom language by using this snippet.

    add_action( 'after_setup_theme', function() {
    
        // Assuming your language files are in /wp-content/themes/your-theme/languages
        // Don't forget to change 'text-domain' to your theme text-domain.
    
        // For Theme.
        load_theme_textdomain( 'text-domain', get_template_directory() . '/languages' );
    
        // For Child Theme
        load_child_theme_textdomain( 'text-domain', get_stylesheet_directory() . '/languages' );
    
        // Remove the load_child_theme_textdomain if you are using it for the main theme,
        // OR Remove load_theme_textdomain if you are loading child theme language.
    
    });
    

    Similarly, you can change language based on URL, or for your plugin page, etc.

    Hope it’ll help. OR let me know if I missed something.

  5. You can also try this:

    1.Download jquery translate script from this link

    2.How it works ( doc, live test )

    <?php
    function ravs_frontend_translate_script() {
        wp_enqueue_script( 'translate', get_template_directory_uri() . '/js/jquery.translate.js', array( 'jquery' ), '0.0.1', true );
    }
    add_action( 'wp_enqueue_scripts', 'ravs_frontend_translate_script' );
    
    
    function ravs_translate_webpage_fx(){
    ?>
    <script>
        // run script after both script load for translate webpage
            jQuery(document).ready(function(){
              //from english to german:
              jQuery('body').translate( 'ar' );
            });
    </script>
    <?php
    add_action('wp_footer','ravs_translate_webpage_fx', 999 ); // make sure this hook run after jquery-translate include in webpage
    
  6. Here is an example code snippet that you can use to change the language dynamically in WordPress:

    add_filter('locale', 'change_user_lang');
    function change_user_lang($locale)
    {
        if (is_admin()) return 'en_US'; // set the admin language to English
        // your custom language logic here, e.g.:
        // if (some condition) return 'fr_FR'; // set the language to French
        // if (some other condition) return 'de_DE'; // set the language to German
    
        return $locale; // return the original language if no conditions are met
    }
    

Comments are closed.