i18N WordPress using mo files not accepting context

I am getting a bit confused with loading my themes .MO file.
I have did a lot of searching on google and I got a version working but I don’t understand why.

First of all, I have set my text domain in the themes CSS file:

Read More
Text Domain:        beauty

I started out with the Roots theme, which does the after_setup_theme and I load the MO file there:

  load_theme_textdomain('beauty', get_template_directory() . '/lang');

This MO file has a ‘beauty’ context and a ‘roots’ context.
The MO file is loading, cause when I dump $l10n[$domain] I see my 3 values the have the beauty context.

For the sake of space I will show only one entry:

array (size=3)
  'beautyInloggen' => 
    object(Translation_Entry)[218]
      public 'is_plural' => boolean false
      public 'context' => string 'beauty' (length=6)
      public 'singular' => string 'Inloggen' (length=8)
      public 'plural' => null
      public 'translations' => 
        array (size=1)
          0 => string 'Login' (length=5)
      public 'translator_comments' => string '' (length=0)
      public 'extracted_comments' => string '' (length=0)
      public 'references' => 
        array (size=0)
          empty
      public 'flags' => 
        array (size=0)
          empty

I have 2 locals in my site, nl_NL and en_UK. This are set using WPML and are loaded correctly.
I have a nl_NL.mo and an en_UK.mo file in the lang folder of the theme.

So, this all looks good.

However, in my header.php I have a login anchor:

<a href="<?php echo $login_url; ?>" target="_self"><?php _e( 'Inloggen', 'beauty' ); ?></a>

which in my understanding should load the context ‘beauty’ and the key ‘Inloggen’.

So, it doesn’t work when I am in my locale nl_UK, I see the standard Dutch text of ‘Inloggen’ instead of ‘Login’.

Strangely, when I remove the msgctxt "beauty" context from my PO file and save it to create a new MO file, it DOES work.

I have used contexts before in my plugin where I use load_textdomain for loading MO files and that works fine.

The reason I want to use contexts is because the one file contains Roots and Beauty related texts and I am only interested in the Beauty texts. I don’t want to remove all the _e entries of the Roots theme so it doesn’t come up when I load sources.

So that was one question.

Question 2 is less important but also puzzling me: When I look at the example of the theme twentyfiftheen the have their PO and MO files in the wp-content/languages/themes/ folder as twentyfiftheen-nl_NL.mo.
I read this great article (https://localise.biz/help/wordpress/loading-translations) and this post (https://wordpress.stackexchange.com/questions/137427/themes-text-domain-language-po-file-not-working) and understood that you should always give the MO file the name of the locale and not of the contextname + locale.

In this nice doc (https://localise.biz/help/wordpress/loading-translations) I find

So in short, WordPress will look in only two places for a theme's MO file:

{absolute_path}/{locale}.mo
wp-content/languages/themes/{domain}-{locale}.mo
e.g. for twentyfourteen's French translations:

wp-content/themes/twentyfourteen/languages/fr_FR.mo
wp-content/languages/themes/twentyfourteen-fr_FR.mo

However, when I had my MO/PO file in the wp-content/languages/themes it wasn’t working, but It could be related to the same issue I have with the context (question 1).

cheers

Related posts

1 comment

  1. Okay, I found out that I abused the Context attribute in POEdit as if it was a domain.
    I was confused by another post thinking that it would work like that.
    Well it doesn’t.

    So I did what @Václav Slavík recommended and wrapped the _e and __ functions into my own functions so that POEdit could search for those end I would not get the Roots language tags in my files.
    Problem solved.
    It also loads from the default language folder of WordPress, it was all related to the context/domain confusion.

Comments are closed.