@@ -260,6 +260,15 @@ public function __construct() {
260260 // Announce that the class is ready, and pass the object (for advanced use).
261261 do_action_ref_array ( 'tgmpa_init ' , array ( $ this ) );
262262
263+ /*
264+ * Load our text domain and allow for overloading the fall-back file.
265+ *
266+ * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA
267+ * generator on the website.}}
268+ */
269+ add_action ( 'init ' , array ( $ this , 'load_textdomain ' ), 5 );
270+ add_filter ( 'load_textdomain_mofile ' , array ( $ this , 'overload_textdomain_mofile ' ), 10 , 2 );
271+
263272 // When the rest of WP has loaded, kick-start the rest of the class.
264273 add_action ( 'init ' , array ( $ this , 'init ' ) );
265274 }
@@ -430,6 +439,90 @@ public function init() {
430439 }
431440 }
432441
442+ /**
443+ * Load translations.
444+ *
445+ * @since 2.x.x
446+ *
447+ * @internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to
448+ * get round the different ways of handling the path and deprecated notices being thrown
449+ * and such. For plugins, the actual file name will be corrected by a filter.
450+ *
451+ * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
452+ * generator on the website.}}
453+ */
454+ public function load_textdomain () {
455+ if ( is_textdomain_loaded ( 'tgmpa ' ) ) {
456+ return ;
457+ }
458+
459+ if ( false !== strpos ( __FILE__ , WP_PLUGIN_DIR ) || false !== strpos ( __FILE__ , WPMU_PLUGIN_DIR ) ) {
460+ // Plugin, we'll need to adjust the file name.
461+ add_action ( 'load_textdomain_mofile ' , array ( $ this , 'correct_plugin_mofile ' ), 10 , 2 );
462+ load_theme_textdomain ( 'tgmpa ' , dirname ( __FILE__ ) . '/languages ' );
463+ remove_action ( 'load_textdomain_mofile ' , array ( $ this , 'correct_plugin_mofile ' ), 10 );
464+ } else {
465+ load_theme_textdomain ( 'tgmpa ' , dirname ( __FILE__ ) . '/languages ' );
466+ }
467+ }
468+
469+ /**
470+ * Correct the .mo file name for (must-use) plugins.
471+ *
472+ * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`.
473+ *
474+ * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
475+ * generator on the website.}}
476+ *
477+ * @since 2.x.x
478+ *
479+ * @param string $mofile Full path to the target mofile.
480+ * @param string $domain The domain for which a language file is being loaded.
481+ * @return string $mofile
482+ */
483+ public function correct_plugin_mofile ( $ mofile , $ domain ) {
484+ // Exit early if not our domain (just in case).
485+ if ( 'tgmpa ' !== $ domain ) {
486+ return $ mofile ;
487+ }
488+ return preg_replace ( '`/([a-z]{2}_[A-Z]{2}.mo)$` ' , '/tgmpa-$1 ' , $ mofile );
489+ }
490+
491+ /**
492+ * Potentially overload the fall-back translation file for the current language.
493+ *
494+ * WP, by default since WP 3.7, will load a local translation first and if none
495+ * can be found, will try and find a translation in the /wp-content/languages/ directory.
496+ * As this library is theme/plugin agnostic, translation files for TGMPA can exist both
497+ * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory.
498+ *
499+ * This method makes sure both directories are checked.
500+ *
501+ * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
502+ * generator on the website.}}
503+ *
504+ * @since 2.x.x
505+ *
506+ * @param string $mofile Full path to the target mofile.
507+ * @param string $domain The domain for which a language file is being loaded.
508+ * @return string $mofile
509+ */
510+ public function overload_textdomain_mofile ( $ mofile , $ domain ) {
511+ // Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable.
512+ if ( 'tgmpa ' !== $ domain || false === strpos ( $ mofile , WP_LANG_DIR ) || @is_readable ( $ mofile ) ) {
513+ return $ mofile ;
514+ }
515+
516+ // Current fallback file is not valid, let's try the alternative option.
517+ if ( strpos ( $ mofile , '/themes/ ' ) !== false ) {
518+ return str_replace ( '/themes/ ' , '/plugins/ ' , $ mofile );
519+ } elseif ( strpos ( $ mofile , '/plugins/ ' ) !== false ) {
520+ return str_replace ( '/plugins/ ' , '/themes/ ' , $ mofile );
521+ } else {
522+ return $ mofile ;
523+ }
524+ }
525+
433526 /**
434527 * Hook in plugin action link filters for the WP native plugins page.
435528 *
0 commit comments