[wp-trac] [WordPress Trac] #52438: Theme translations in WP_LANG_DIR are loaded twice, no (logical) way to override from a (child) theme.

WordPress Trac noreply at wordpress.org
Wed Feb 3 19:21:46 UTC 2021


#52438: Theme translations in WP_LANG_DIR are loaded twice, no (logical) way to
override from a (child) theme.
---------------------------+-----------------------------
 Reporter:  captain.crash  |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  I18N           |    Version:  5.6
 Severity:  minor          |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 I was trying to override some translation strings from within a child
 theme for a (parent) theme that has online translations (from
 translate.wordpress.org) that WordPress downloaded into WP_LANG_DIR/themes
 /domain-locale.mo.

 There are a couple of problems getting this to work, at least one of which
 (2)) I believe is a bug.

 1) Both `load_theme_textdomain()` and consequently
 `load_child_theme_textdomain()` will give priority to .mo files found in
 WP_LANG_DIR and will completely ignore theme based .mo files when the
 former exist. I tried to circumvent this with `load_textdomain()`
 directly, see [https://wordpress.stackexchange.com/a/249127 here] and made
 it load before the parent theme's call to `load_theme_textdomain()` in
 order to make my translations
 [https://core.trac.wordpress.org/browser/tags/5.6/src/wp-
 includes/l10n.php#L684 take precedence in the merge]. I did like proposed
 in the docs, i.e. in the child theme's functions file (that loads before
 the parent's) via the `after_setup_theme` hook.
 The question here is should there be a better (more intuitive) way? Maybe
 calling `load_theme_textdomain()` with the parent's text domain again (and
 make it work, of course)?

 2) It did not seem to work, so I tried to log which .mo files WordPress
 tries loading with
 [https://developer.wordpress.org/reference/hooks/load_textdomain/#comment-4497
 this code]. This showed that the downloaded translation in WP_LANG_DIR
 gets called twice. The 2nd call is from the parent theme's
 `load_theme_textdomain()` and my effort from 1) successfully put my
 translaions before that 2nd call. But the 1st .mo file loading... I traced
 this down to [https://core.trac.wordpress.org/browser/tags/5.6/src/wp-
 includes/l10n.php#L1307 this code] within `l10n.php`: the check with
 `_load_textdomain_just_in_time()` actually loads the .mo file via
 `load_textdomain()` and way to early. And anyway, does this check make any
 sense:

 {{{
 if ( isset( $l10n[ $domain ] ) || ( _load_textdomain_just_in_time( $domain
 ) && isset( $l10n[ $domain ] ) ) )
 }}}

 If `isset( $l10n[ $domain ] )` is `false`, how could the condition after
 `||` ever be `true`?
 Removing the OR condition with the call to
 `_load_textdomain_just_in_time()` also removes the 1st logged .mo file.
 Then, my translations from the child theme were the first to load and
 worked like intended, i.e. merge with the parent's translations that come
 afterwards.

 It also works when calling `load_textdomain()` in the (child) themes
 functions file directly (not bound to a hook), but that's not how I
 understand that this should be done?!

 I reproduced the double loading .mo files with a fresh WordPress 5.6
 install and the default Twenty Twenty-One theme in formal German language.

 I hope, I did not miss anything crucial and this report is of any help.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/52438>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list