Skip to main content
Updated `locale` portion of PHP code to showcase additional attempts at getting this right ¯\_(ツ)_/¯
Source Link
<?php
// set the locale to use
add_filter('locale', function () {
    return 'ja_JP';
    // return 'ja';          // tried this as well, no difference :(
});

// load the textdomain for translation
add_action('after_setup_theme', function () {
    $path = get_stylesheet_directory();
    load_child_theme_textdomain('mysite', $path);
});

function mysite_jp_scripts()
{
    // enqueue style
    wp_enqueue_style('master-jp-css', get_stylesheet_directory_uri().'/library/css/master-jp.min.css');
}
add_action('wp_enqueue_scripts', 'mysite_jp_scripts', 12);
<?php
// set the locale to use
add_filter('locale', function () {
    return 'ja_JP';
});

// load the textdomain for translation
add_action('after_setup_theme', function () {
    $path = get_stylesheet_directory();
    load_child_theme_textdomain('mysite', $path);
});

function mysite_jp_scripts()
{
    // enqueue style
    wp_enqueue_style('master-jp-css', get_stylesheet_directory_uri().'/library/css/master-jp.min.css');
}
add_action('wp_enqueue_scripts', 'mysite_jp_scripts', 12);
<?php
// set the locale to use
add_filter('locale', function () {
    return 'ja_JP';
    // return 'ja';          // tried this as well, no difference :(
});

// load the textdomain for translation
add_action('after_setup_theme', function () {
    $path = get_stylesheet_directory();
    load_child_theme_textdomain('mysite', $path);
});

function mysite_jp_scripts()
{
    // enqueue style
    wp_enqueue_style('master-jp-css', get_stylesheet_directory_uri().'/library/css/master-jp.min.css');
}
add_action('wp_enqueue_scripts', 'mysite_jp_scripts', 12);
Updated with language files naming example
Source Link

Updated 2020-06-14 22:43

The language files in the parent theme look like this:

/app/public_html/wp-content/languages/loco/themes/mysite-de_DE.mo
/app/public_html/wp-content/languages/loco/themes/mysite-de_DE.po
/app/public_html/wp-content/languages/loco/themes/mysite-fr_FR.mo
/app/public_html/wp-content/languages/loco/themes/mysite-fr_FR.po
/app/public_html/wp-content/languages/loco/themes/mysite-ja.mo
/app/public_html/wp-content/languages/loco/themes/mysite-ja.po
/app/public_html/wp-content/languages/loco/themes/mysite-ko_KR.mo
/app/public_html/wp-content/languages/loco/themes/mysite-ko_KR.po

Updated 2020-06-14 22:43

The language files in the parent theme look like this:

/app/public_html/wp-content/languages/loco/themes/mysite-de_DE.mo
/app/public_html/wp-content/languages/loco/themes/mysite-de_DE.po
/app/public_html/wp-content/languages/loco/themes/mysite-fr_FR.mo
/app/public_html/wp-content/languages/loco/themes/mysite-fr_FR.po
/app/public_html/wp-content/languages/loco/themes/mysite-ja.mo
/app/public_html/wp-content/languages/loco/themes/mysite-ja.po
/app/public_html/wp-content/languages/loco/themes/mysite-ko_KR.mo
/app/public_html/wp-content/languages/loco/themes/mysite-ko_KR.po
Source Link

String translation within WordPress multisite with i18n not working on all sites

Using

  • WordPress Multisite (subdir)
  • Child themes
  • Loco Translate

The Issue

One of our multisite sites isn't string translating (Japanese). We started with non-multisite with just English. Then, converted to multisite and introduced German. The idea was each multisite would serve as a locale. Each multisite site would receive its own theme inheritance from the original theme. String translation seemed to work correctly after some debugging and fine tuning. Later, we added French and Korean and that was a breeze. Now, Japanese isn't working for us. The other languages do, though.

We utilize Loco Translate to handle the translating locally, then commit the generated .po and .mo files in Git before deployment. The translations have already been put into the Japanese side and things just aren't working to replace template strings like the other languages handled it. I've set locale in the dashboard as well; doesn't help.

I've checked everything I can think of, and looped in other developers to no avail. The textdomain matches and the locale is set. The language files for reading in are being correctly created and put in the right places. I'm actually starting to wonder if this is an mbstring thing of some sort; like maybe the regular translation functions in Core aren't handling multibyte characters or something. But I have to imagine they've already thought of that in the __() function, right? That's why we use the textdomain and the files; which need to match up in order to get a text swap. Only, that text swap behavior isn't happening for Japanese. We can put Japanese text into the WYSIWYG textareas content-wise, but it's the theme strings I'm trying to get handled here.

Really running out of ideas on this one. Hoping someone might have some idea of what's going on. 🤷🏼‍♂️

Site Structure

/                (English, multisite parent)
/de/             (German, multisite child)
/fr/             (French, multisite child)
/kr/             (Korean, multisite child)
/jp/             (Japanese, multisite child)

Themes

Each of the sites has a child theme enabled for itself. We have deviating logic for these themes/locales sometimes, depending on the locale and what's going on, so we do have a separate child theme for each "site" within the multisite network. Even the English/root site has a child theme that inherits the parent theme's logic; so they all get the overall parent site.

The themes look like this:

/app/public_html/wp-content/themes/mysite             (parent theme, inherited)
/app/public_html/wp-content/themes/mysite-child       (English, child theme)
/app/public_html/wp-content/themes/mysite-child-de    (German, child theme)
/app/public_html/wp-content/themes/mysite-child-fr    (French, child theme)
/app/public_html/wp-content/themes/mysite-child-kr    (Korean, child theme)
/app/public_html/wp-content/themes/mysite-child-jp    (Japanese, child theme)

Example of the Japanese child theme functions.php file:

<?php
// set the locale to use
add_filter('locale', function () {
    return 'ja_JP';
});

// load the textdomain for translation
add_action('after_setup_theme', function () {
    $path = get_stylesheet_directory();
    load_child_theme_textdomain('mysite', $path);
});

function mysite_jp_scripts()
{
    // enqueue style
    wp_enqueue_style('master-jp-css', get_stylesheet_directory_uri().'/library/css/master-jp.min.css');
}
add_action('wp_enqueue_scripts', 'mysite_jp_scripts', 12);

Example of syle.css for the child theme:

/*
 Theme Name:   My Site Theme: JP
 Theme URI:    https://example.com
 Description:  My Site Theme: JP
 Author:       Me
 Author URI:   https://example.com
 Template:     mysite
 Version:      1.0.0
 License:      GNU General Public License v2 or later
 License URI:  http://www.gnu.org/licenses/gpl-2.0.html
 Text Domain:  mysite
*/