roots / bedrock

WordPress boilerplate with Composer, easier configuration, and an improved folder structure
https://roots.io/bedrock/
MIT License
6.16k stars 1.16k forks source link

Translation hook with bedrock-autoloader #117

Closed hyyan closed 9 years ago

hyyan commented 9 years ago

What hook should I use to load translation when I use bedrock-autoloader to load my plugin ?

Foxaii commented 9 years ago

Can you expand on the issue you are encountering please? I'm not 100% sure what you mean.

You could also try the changes in #110 that make the autoloaded plugins load first instead of last (but be mindful of the caveats I mention in that PR).

hyyan commented 9 years ago

@Foxaii What I am trying to do is to load translation file for my plugin which must be loaded by bedrock-autoloader , the plugin is simple branding plugin , Here is a snippet :

/*
 * This file is part of the SARC package.
 * (c) Hyyan Abo Fakher@Guestra <tiribthea4hyyan@gmail.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 * 
 * Plugin Name: Guestra Branding
 * Description: Brand this wordpress installation for Guestra
 * Author: Guestra
 * Author URI: http://www.guestra.net
 */

add_filter('Hyyan\Dashboard.options', 'guestra_dashboard_plugin');
add_action('admin_bar_menu', 'geustra_contact_menu', 9999);
add_action('plugins_loaded', 'guestra_lang');

/**
 * Add the plugin translation
 */
function guestra_lang() {
    load_plugin_textdomain(
            'guestra'
            , false
            , trailingslashit(basename(dirname(__FILE__))) . 'languages/'
    );
}

Now as you can see from the snippet the translation files will not be loaded because the autoloader itself use plugins_loaded hook to make its job done . and the question is what hook should I use instead ??

Note : the plugin works fine if I use it as standard wordpress plugin offcourse

Foxaii commented 9 years ago

It would be helpful if you could try two fixes and report back which worked (it should be both):

  1. Change the plugins_loaded action to muplugins_loaded here
  2. Try modifying the autoloader as per #110
hyyan commented 9 years ago

@Foxaii I've just tried what you proposed to do , But it didn't work

Foxaii commented 9 years ago

@hyyan It will take some further digging. I'll have a look into it tomorrow or over the weekend.

Foxaii commented 9 years ago

This isn't anything autoloader related.

If you look at the parameters of load_plugin_textdomain the full path option has been deprecated, so the only path you can use must be relative to the plugin directory, not the mu-plugin directory.

hyyan commented 9 years ago

@Foxaii I know that , but the method in my snippet return relative path not a full path , I am already aware of the second deprecated param . What we are missing here is the right hook to load translation file , in normal plugins we use load_plugin_textdomain and in mu-plugins we use load_muplugin_textdomain but none of these methods works with autoloader , Is there any alternative hook to use with autloader so the translation file will be loaded ?

hyyan commented 9 years ago

@Foxaii I think there is must a bedrock autoloader hook like wordpress native hooks to mark that plugins have loaded by the autoloader for example bedrock_plugins_loaded , but I am not really sure if something like that will solve the problem , What do you think?

Foxaii commented 9 years ago

Your translation files are in the mu-plugins directory, but the load_plugin_textdomain function takes the relative path to those files and prepends the path to the plugins directory, so it's not looking where the translation files are.

If you apply the changes in #110 and then use the load_muplugin_textdomain hook it should work, as it will be looking in the correct folder.

I'm not sure if additional hooks are the answer but will look into it.

hyyan commented 9 years ago

@Foxaii It is still not working , even with #110 changes, I don't why ??? I think I will use the plugin as standard wordpress plugin for now until the solution become available , Thanks anyway

Foxaii commented 9 years ago

I'm not sure if you ever got this working as you intended, but the way I use translations is by creating an languages/plugin folder in app that contains my-plugin-name-en_GB.mo and my-plugin-name-en_GB.mo.

chimok commented 4 months ago

@Foxaii Almost 10 years later I have the same trouble. :) The mu-plugin is with stalled with composer. Create app/languages/plugin is not a nice solution, i want my transations inside my plugin directory.