[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
Tue Nov 26 10:42:16 UTC 2024


#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:  closed
 Priority:  normal                     |   Milestone:
Component:  I18N                       |     Version:  5.6
 Severity:  minor                      |  Resolution:  worksforme
 Keywords:  needs-patch needs-testing  |     Focuses:
---------------------------------------+-------------------------
Changes (by swissspidy):

 * status:  reopened => closed
 * resolution:   => worksforme


Comment:

 > What is the condition, now found here, supposed to be doing? If isset(
 $l10n[ $domain ] ) is false, how could the condition after || ever be
 true?

 `_load_textdomain_just_in_time()` calls `load_textdomain`, which modifies
 the global `$l10n[ $domain ]` variable. So if the loading is successful,
 that next condition will be true.

 > I'm trying to override some translation strings from within a child
 theme for a (parent) theme that has online translations stored into
 WP_LANG_DIR/themes/parentdomain-locale.mo. I put my divergent translations
 for the parent theme into my child theme CHILD/languages/parentdomain-
 locale.mo, so I do not lose them during updates. Obviously, I want these
 to take precedence. How am I supposed to make these load before the ones
 in WP_LANG_DIR/themes/domain-locale.mo?

 Let's say you are building a child theme for `twentyten`.

 In your child theme's `functions.php` you will use code like this:

 {{{#!php
 <?php
 function twentyten_child_override_parent_translations() {
         $locale = determine_locale();
         load_textdomain( 'twentyten', get_stylesheet_directory() .
 "/languages/twentyten-$locale.mo" );
 }

 add_action( 'after_setup_theme',
 'twentyten_child_override_parent_translations' );
 }}}

 Then, create `languages/twentyten-de_DE.l10n.php` (yes, a PHP file, but MO
 would work too) in your child theme's directory to override the `de_DE`
 translation (to give an example). The file could look like this:

 {{{#!php
 <?php
 return array(
         'x-generator'               => 'GlotPress/4.0.1',
         'translation-revision-date' => '2024-08-02 14:38:38+0000',
         'plural-forms'              => 'nplurals=2; plural=n != 1;',
         'project-id-version'        => 'Themes - Twenty Ten',
         'language'                  => 'de',
         'messages'                  => array(
                 'Apologies, but the page you requested could not be found.
 Perhaps searching will help.' => 'My new not found string',
                 'Not Found' => 'My new not found string',
         ),
 );
 }}}

 This will override the "Not Found" string on the 404 page.

 Nothing will be loaded twice.


 ----

 Note that I am closing this again, but conversation can continue on closed
 tickets as well.

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


More information about the wp-trac mailing list