Found a timing bug of Text domain loading with suggested validation method
-
Hello!
I am trying to develop something for a client site. It has the following configuration. To be simple, I sum up the settings that can help to test out the problem with reproducing steps, possible cause and possible solutions for your review.
Plugin list
- WooCommerce
- qTranslate-XT
- Advanced WooCommerce Search
Language settings
- English
- Chinese Traditional (Hong Kong) *default
- Chinese Simplified (China)
Problem found
When translation mo/po are added to WP_CONTENT/languages/plugins with
advanced-woo-search-zh_CN.mo
advanced-woo-search-zh_CN.po
advanced-woo-search-zh_HK.mo
advanced-woo-search-zh_HK.poThe zh_HK is always loaded and will display the same language when even choosing other languages.
Affected area
Only affect the AWS translation while other shows normally.Possible caused found during code analysis
In AWS code, the AWS is called in the hook cycle “woocommerce_loaded”, there is a function load_plugin_textdomain(). If putting the following code right after load_plugin_textdomain to test out
the determined language is always default language zh_HK// ... after load_plugin_textdomain() $locale = determine_locale(); var_dump( $locale); exit; // it is always "zh_HK" // ...
To find out he problem, tried to test the same strings with other Text Domain. eg
languages regardless of chosen language in gTranslate-XT language menu.var_dump(__('Tag', 'woocommerce')); // Display chosen translation. var_dump(__('Search', 'woocommerce')); // Display chosen translation. var_dump(__('Search', 'advanced-woo-search')); // Always display same
The “find out” is by comparing WooCommerce text domain loading sequence. It is in cycle hook “init”.
// in the class-woocommerce.php load_plugin_textdomain() { // ... var_dump( $locale); exit; // Display chosen language code. // ...
According to this finding, the possible solution is, as a quick POC:
// ... add_action( 'woocommerce_loaded', 'aws_init' ); // Move load_plugin_textdomain() here instead. add_action( 'init', function() { $locale = determine_locale(); var_dump( $locale); exit; // Display chosen language. Because load_plugin_textdomain() depends on the same logic of determine_locale(). } );
Hope this could help someone else who is struggling in getting right translation with the AWS plugin.
Workaround
// Put the following code in eg. functions.php in a theme. Don't forget to put the translation into WP_CONTENT_DIR/languages/plugins/ with correct text domain name and desired language code in the filename. // Workaround to address AWS text domain sequence problem. add_action( 'init', function() { unload_textdomain( 'advanced-woo-search' ); load_plugin_textdomain( 'advanced-woo-search', false, false ); });
- The topic ‘Found a timing bug of Text domain loading with suggested validation method’ is closed to new replies.