• I’d like to use some variation on the __() function, where I not only specify the key and the namespace, but also the language to which I want to translate to. Or, otherwise, can I set the locale to the desired one before calling the function and then back to the previous one after that? So something like

    
    global $locale;
    $pre_locale = $locale;
    $locale = 'en_us';
    __('Key', 'namespace');
    $locale = $pre_locale;
    
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    You could alter the return of translation functions through the “gettext” filter. But there is no way to pass a preference through __() and the like, it’d have to come from elsewhere.

    If you’re getting the preference from elsewhere, you can alter the chosen language through the “locale” filter. Of course you can only change to locales who have their language pack installed on the site.

    Thread Starter Guido Goluke

    (@majorlabel)

    Thanks for your reaction. I’ve little to no experience with wordpress translation. I looked up the information about the locale hook. I figured I should be able to use do_action( 'switch_locale', 'de_DE' ); to switch to that language (after which the __() and _e() functions should use that localization), but in this specific case, we’re talking about a plugin that has it’s own textdomain. It’s a PDF generator where I would like to switch some translations based on the customer language. I’ve applied the action, but no success, any hints on how I can debug this?

    • This reply was modified 4 years, 10 months ago by Guido Goluke.
    Moderator bcworkz

    (@bcworkz)

    More like

    add_action('locale', function(){
       return 'de_DE';
    });

    But you’ll need a conditional check to prevent German from always being used. Something to only return ‘de_DE’ or the chosen language when creating a PDF.

    Thread Starter Guido Goluke

    (@majorlabel)

    I tried that construction, didn’t work. Where is that locale code going to look? The namespace is set to the plugin name, so will the _e() function look in the plugins directory as well? And will it look in the ‘languages’ directory? And does it expect a certain filename? I think you’re right in that it needs to use the locale filter, but it’s not doing what I expect it to and I don’t exactly know how this functionality works so I’m shooting in the dark here.

    • This reply was modified 4 years, 10 months ago by Guido Goluke.
    Moderator bcworkz

    (@bcworkz)

    TBH, I’m not super knowledgeable on how the gettext translation scheme works. _e() and all of its relatives all utilize gettext. AFAIK gettext first looks for translation files in /wp-content/languages/. The file it looks for is named using the text domain in combination with the current locale returned by get_locale(), which is filtered through “locale”. If no such file is found there, gettext then looks in the plugin folder for a qualifying language file.

    You do have to add the filter before gettext tries to access any translation files. I placed my example code in one of my English test site plugins and the entire site became German (the parts coming from WP anyway), so it does work in that context. I’m not sure how it would only be applied to PDF generation since I’m unfamiliar with your plugin.

    Thread Starter Guido Goluke

    (@majorlabel)

    Wow, missed the e-mail there. Thanks for your answer, will inspect.

    Thread Starter Guido Goluke

    (@majorlabel)

    OK, so the fact that you pointed me to look in wp-content/languages/plugins was a great help. I rather forced it by using switch_to_locale(), that worked. Obviously I used the desired locale as a parameter there, something like ‘en_GB’

    • This reply was modified 4 years, 9 months ago by Guido Goluke.
Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Forcing translation destination language’ is closed to new replies.