• I still have to find a solution to this problem about the navigation bar, top light blue bar on the right side. If I set the site admin language in german I see the words in german (the plug in has gettext), but if have the admin in italian and load the subdomain for german version: de.test.sacconicase.com I cant see the words in german. I’d like to re-work on this subject, I suppose I have to add some code in the (free) plug-in WP-PageNavi

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 64 total)
  • Moderator bcworkz

    (@bcworkz)

    You shouldn’t need to alter the plugin if its gettext is at least working under some circumstances. It may be you’re not filtering the locale to “de_DE” soon enough for plugin gettext to work as you want. Plugins load very early in the process. Another way to set locale is by defining the “WPLANG” constant. IIRC, you have code in wp-config.php that sets site and home URL constants to match the requested HTTP_HOST or SERVER_NAME (in $_SERVER)?

    In the related wp-config.php code, when the HTTP_HOST or SERVER_NAME includes the “de.” subdomain, do this:
    define ('WPLANG', 'de_DE');

    Thread Starter sacconi

    (@sacconi)

    I have just this in my wp-config.php

    //configurazione sottodominio in tedesco
    
    define( 'WP_SITEURL', 'https://' .  $_SERVER['HTTP_HOST']);
    
    define( 'WP_HOME', 'https://' .  $_SERVER['HTTP_HOST']); 
    

    I simply add define ('WPLANG', 'de_DE'); ? But in this way:

    define( 'WP_SITEURL', 'https://' .  $_SERVER['HTTP_HOST']), ('WPLANG', 'de_DE');
    
    define( 'WP_HOME', 'https://' .  $_SERVER['HTTP_HOST']), ('WPLANG', 'de_DE');

    or in this other:

    define( 'WP_SITEURL', 'https://' .  $_SERVER['HTTP_HOST']);
    
    define( 'WP_HOME', 'https://' .  $_SERVER['HTTP_HOST']);
    
    define ('WPLANG', 'de_DE');

    Moderator bcworkz

    (@bcworkz)

    You cannot append different PHP statements with a comma separator. Each must end with a semi-colon. While you can have multiple statements on one line, it’s better if each is on its own line.

    The order doesn’t matter, but you need a conditional based on “de.” subdomain, otherwise you’ll always see German, even for your Italian site.

    if ('de.' == substr( $_SERVER['HTTP_HOST']), 0, 3 ) {
       define ('WPLANG', 'de_DE');
    }
    Thread Starter sacconi

    (@sacconi)

    Using

    if ('de.' == substr( $_SERVER['HTTP_HOST']), 0, 3 ) {
       define ('WPLANG', 'de_DE');
    }

    after

    define( 'WP_SITEURL', 'https://' .  $_SERVER['HTTP_HOST']);
    
    define( 'WP_HOME', 'https://' .  $_SERVER['HTTP_HOST']);

    makes the site invisible. Error 404 I think, or 505, I dont remeber

    Moderator bcworkz

    (@bcworkz)

    Sorry, my bad, a ) is in the wrong place. First line should be:
    if ('de.' == substr( $_SERVER['HTTP_HOST'], 0, 3 )) {

    Thread Starter sacconi

    (@sacconi)

    Unlickyly it has no effect on the navigation bar, which is still sensitive only to the site admin language

    Moderator bcworkz

    (@bcworkz)

    Well that’s discouraging. I guess the nav plugin isn’t conforming to usual WP conventions. I wonder how it’s deciding what language to use. I suspect it’s getting the WPLANG option straight from the DB and ignoring any constants or locale settings. If it’s using get_option() to do this, we could use the “pre_option_WPLANG” filter to return “de_DE” regardless of what the actual setting is. It’d be similar to how you filter the locale based on the requested URL.

    Thread Starter sacconi

    (@sacconi)

    Maybe it’s more simple if I do my own navigation/pagination code with gettext. There is a track somewhere?

    Moderator bcworkz

    (@bcworkz)

    That does not sound that simple. It’s at least worth trying the “pre_option_WPLANG” filter first. If that doesn’t help any, then I’m out of ideas on getting the nav plugin to cooperate. Then your choices are to either alter the nav plugin code yourself or develop your own nav functions. The starter theme you began with has at least some basic navigation functionality you could start with.

    Thread Starter sacconi

    (@sacconi)

    I have found the following: first I should create a file: force-locale.php (or I have to put it in wp-config.php or in functions.php ?)

    and then use

    <?php
    WP_CLI::add_wp_hook( 'pre_option_WPLANG', function() {
        return 'de_DE';
    });

    What I should change here?

    Moderator bcworkz

    (@bcworkz)

    Do you have wp-cli installed? Even if you do, it’s not necessary. Try adding a normal filter hook in functions.php.

    add_filter('pre_option_WPLANG', 'sac_set_lang');
    function sac_set_lang( $lang ) {
       if ('de.' == substr( $_SERVER['HTTP_HOST'], 0, 3 )) return 'de_DE';
       return $lang;
    }

    There’s a chance doing this in functions.php is too late in the process. It can be executed earlier by placing it in a plugin. A custom plugin can be a single .php file in the /plugins/ folder. It must have certain comment header information.

    Thread Starter sacconi

    (@sacconi)

    I created a plugin with this basic content

    /*
     * Plugin Name: NavBar-lingue
     */
    // NAV BAR IN TEDESCO
    
    add_filter('pre_option_WPLANG', 'sac_set_lang');
    function sac_set_lang( $lang ) {
       if ('de.' == substr( $_SERVER['HTTP_HOST'], 0, 3 )) return 'de_DE';
       return $lang;
    }
    

    it’s in folder inside the plugins folder, but it’s not working, the nav bar still outputs italian names also in the german version

    Moderator bcworkz

    (@bcworkz)

    I’ve confirmed that another plugin which does gettext correctly does output translations when only the “locale” filter is used. So either the nav plugin is doing something odd, or another plugin is interfering. Do you use any kind of translation plugin? It might be overriding our own locale filter. As a test, deactivate the translation plugin to see if it causes the nav plugin behave. If it does, there might be something else we could try.

    Thread Starter sacconi

    (@sacconi)

    I deactivated loco translate but I still have terms in italian on the german version 🙁

    Moderator bcworkz

    (@bcworkz)

    I glanced through the plugin’s source code. I think what’s happening is its translations are loaded very early, before our locale filter is applied. If so, the solution is attempt to filter locale even earlier. It will need to be done from a plugin. Try moving your current locale filter from your theme to the plugin you recently created.

    In its add_filter() call, add a third argument: 0
    Like this: add_filter('locale', 'your-callback-name-here', 0 );

Viewing 15 replies - 1 through 15 (of 64 total)
  • You must be logged in to reply to this topic.