Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loading of TGMPA native translation files. #521

Merged
merged 1 commit into from
Jan 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ public function __construct() {
// Announce that the class is ready, and pass the object (for advanced use).
do_action_ref_array( 'tgmpa_init', array( $this ) );

/*
* Load our text domain and allow for overloading the fall-back file.
*
* {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA
* generator on the website.}}
*/
add_action( 'init', array( $this, 'load_textdomain' ), 5 );
add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 );

// When the rest of WP has loaded, kick-start the rest of the class.
add_action( 'init', array( $this, 'init' ) );
}
Expand Down Expand Up @@ -430,6 +439,90 @@ public function init() {
}
}

/**
* Load translations.
*
* @since 2.x.x
*
* (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to
* get round the different ways of handling the path and deprecated notices being thrown
* and such. For plugins, the actual file name will be corrected by a filter.}}
*
* {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
* generator on the website.}}
*/
public function load_textdomain() {
if ( is_textdomain_loaded( 'tgmpa' ) ) {
return;
}

if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) {
// Plugin, we'll need to adjust the file name.
add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 );
load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 );
} else {
load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
}
}

/**
* Correct the .mo file name for (must-use) plugins.
*
* Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`.
*
* {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
* generator on the website.}}
*
* @since 2.x.x
*
* @param string $mofile Full path to the target mofile.
* @param string $domain The domain for which a language file is being loaded.
* @return string $mofile
*/
public function correct_plugin_mofile( $mofile, $domain ) {
// Exit early if not our domain (just in case).
if ( 'tgmpa' !== $domain ) {
return $mofile;
}
return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile );
}

/**
* Potentially overload the fall-back translation file for the current language.
*
* WP, by default since WP 3.7, will load a local translation first and if none
* can be found, will try and find a translation in the /wp-content/languages/ directory.
* As this library is theme/plugin agnostic, translation files for TGMPA can exist both
* in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory.
*
* This method makes sure both directories are checked.
*
* {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
* generator on the website.}}
*
* @since 2.x.x
*
* @param string $mofile Full path to the target mofile.
* @param string $domain The domain for which a language file is being loaded.
* @return string $mofile
*/
public function overload_textdomain_mofile( $mofile, $domain ) {
// Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable.
if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) {
return $mofile;
}

// Current fallback file is not valid, let's try the alternative option.
if ( false !== strpos( $mofile, '/themes/' ) ) {
return str_replace( '/themes/', '/plugins/', $mofile );
} elseif ( false !== strpos( $mofile, '/plugins/' ) ) {
return str_replace( '/plugins/', '/themes/', $mofile );
} else {
return $mofile;
}
}

/**
* Hook in plugin action link filters for the WP native plugins page.
*
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<file>class-tgm-plugin-activation.php</file>
<file>example.php</file>
<arg name="report" value="full"/>
<arg value="sp"/>
<arg value="spn"/>
<rule ref="WordPress">
<exclude name="WordPress.VIP" />
</rule>
Expand Down