wordpress user
wordpress user

Reputation: 584

Load textdomain from global languages directory [Wordpress]

I am creating translations for my plugin.
To fetch .mo files from my-plugin/languages directory , I use the function

//Load translation
function load_plugin_textdomain() {
    load_plugin_textdomain( 'my-plugin', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'load_plugin_textdomain' );

How can i set the location to wp-content/languages
Setting the location to ABSPATH.'/wp-content/languages' doesn't work.

Upvotes: 1

Views: 1878

Answers (2)

wordpress user
wordpress user

Reputation: 584

If anybody else is looking for the anwser , Here it is

function load_txtdomain() {
    $locale = apply_filters( 'plugin_locale', determine_locale(), 'my-plugin' );
    load_textdomain( 'my-plugin', WP_LANG_DIR . '/my-plugin-' . $locale . '.mo' );
}
add_action('plugins_loaded','load_txtdomain');

Upvotes: 1

Tim
Tim

Reputation: 8616

I'm aware you've accepted your own answer, but although it will work it's against WordPress standards.

The load_plugin_textdomain will load from the global languages directory without modification and should be used for bootstrapping plugin translations. The path you specify as the third argument is a fallback to be used in case the global language file is not installed.

To clarify how it works: WordPress will look in the global languages directory strictly at <WP_LANG_DIR>/plugins/my-plugin-<locale>.mo. So as long your text domain and locale code are correct, it will be loaded.

If it isn't found, WordPress will look at the path you've specified which must be relative to the wp-content/plugins directory.

Historically (prior to WP 4.6) the third argument was loaded first, but WordPress decided to swap the order, favouring community translations (globally installed) over author-provided translations (shipped with plugin ).

Upvotes: 2

Related Questions