Forked from justintadlock/one-theme-two-textdomains.php
Created
August 6, 2017 16:04
-
-
Save dariodev/3644d258bb2d6be5c7ded652beb50337 to your computer and use it in GitHub Desktop.
How one theme can have two textdomains and still work with translation systems.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The purpose of this file is to show how a theme can have multiple textdomains | |
* and still work with a single translation file. Translation tools like the | |
* one used on WordPress.org and Poedit are going to simply scan the project for | |
* text strings, regardless of the textdomain (and for good reasons that are | |
* not relevant here). | |
* | |
* The code below works with that system. It assumes that we have a single MO | |
* file based on the theme's textdomain. So, how can two textdomains work? It | |
* does this by assigning the loaded strings in the MO file to the framework | |
* textdomain (remember that the translation system captures all strings no matter | |
* the textdomain). That way, when a string is called using the framework | |
* textdomain, the string will be properly translated. | |
*/ | |
add_action( 'after_setup_theme', 'jt_load_textdomains' ); | |
function jt_load_textdomains() { | |
// Load theme textdomain. | |
load_theme_textdomain( 'jt-theme', get_template_directory() . '/languages' ); | |
// Load the framework textdomain. | |
load_textdomain( 'jt-framework', '' ); | |
} | |
add_filter( 'override_load_textdomain', 'jt_override_load_textdomain', 10, 2 ); | |
function jt_override_load_textdomain( $override, $domain ) { | |
// Check if the domain is our framework domain. | |
if ( 'jt-framework' === $domain ) { | |
global $l10n; | |
// If the theme's textdomain is loaded, assign the theme's translations | |
// to the framework's textdomain. | |
if ( isset( $l10n[ 'jt-theme' ] ) ) | |
$l10n[ $domain ] = $l10n[ 'jt-theme' ]; | |
// Always override. We only want the theme to handle translations. | |
$override = true; | |
} | |
return $override; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment