[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 Nov 27 22:29:31 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:
---------------------------------------+-------------------------

Comment (by swissspidy):

 I think I used an incorrect hook priority in that example, while I was
 working on some other i18n stuff. There are some new i18n changes in 6.8
 compared to 6.7, which already had some big changes.

 Here's what I just successfully tested:

 {{{#!php
 <?php

 // languages/de_DE.l10n.php: Child theme translations.
 // languages/twentyten-de_DE.l10n.php: Overrides for the parent theme
 translations.

 function twentyten_child_setup() {
         // First, load our translation overrides for the parent theme,
 then load the its original translation.
         $locale = determine_locale();
         load_textdomain( 'twentyten', get_stylesheet_directory() .
 "/languages/twentyten-$locale.mo" );
         load_textdomain( 'twentyten', WP_LANG_DIR .
 "/themes/twentyten-$locale.mo" );

         // Load this child theme's translations.
         // Note: Calling load_theme_textdomain() is no longer necessary
 with 6.8-alpha (trunk), see https://core.trac.wordpress.org/ticket/62244.
         if ( version_compare( $GLOBALS['wp_version'], '6.8-alpha', '<' ) )
 {
                 load_theme_textdomain( 'twentyten-child',
 get_stylesheet_directory() . '/languages' );
         }

         // To test, translate a string from the child theme, and one from
 the parent theme.
         echo __( 'Not Found', 'twentyten-child' );
         echo __( 'Not Found', 'twentyten' );
 }

 add_action( 'after_setup_theme', 'twentyten_child_setup' );

 // Just to make stylesheets working.
 function twentyten_child_enqueue_styles() {
         $parent_style = 'twentyten-style';
         wp_enqueue_style( $parent_style, get_template_directory_uri() .
 '/style.css' );
         wp_enqueue_style(
                 'twentyten-child-style',
                 get_stylesheet_directory_uri() . '/style.css',
                 array( $parent_style ),
                 wp_get_theme()->get( 'Version' )
         );
 }

 add_action( 'wp_enqueue_scripts', 'twentyten_child_enqueue_styles' );

 }}}

 This causes the following translation files to be loaded:

 * `wp-content/themes/twentyten-child/languages/twentyten-de_DE.l10n.php` -
 Your overrides for the parent theme translations
 * `wp-content/languages/themes/twentyten-de_DE.l10n.php` - Parent theme
 translations
 * `wp-content/themes/twentyten-child/languages/de_DE.l10n.php` - Child
 theme translations

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


More information about the wp-trac mailing list