Advertisement:

Author Topic: Translating Plugins  (Read 5051 times)

e2

  • Newbie
  • *
  • Posts: 7
Translating Plugins
« on: January 26, 2011, 12:53:35 pm »
Hi Guys,

New to the forum, new to OSClass.

I am currently using "Real Estate Attribute" plugin and a couple of others I've created.

I was wondering how do I go about translating them to other languages as they are not part of the po/mo files?

Regards,
e2.

Juan Ramón

  • Osclass Developer
  • Hero Member
  • *****
  • Posts: 2382
Re: Translating Plugins
« Reply #1 on: January 26, 2011, 01:28:37 pm »
I've just answered your email, but I put my explanation here too.

You've to translate with poedit using as keywords __ and _e You should choose the plugin folder. However, there's a problem, we don't load .mo files from plugins/themes folders. We've a ticket open: http://issues.osclass.org/browse/OSCLASS-219 I would do as soon as possible.

e2

  • Newbie
  • *
  • Posts: 7
Re: Translating Plugins
« Reply #2 on: January 26, 2011, 06:09:43 pm »
Hi,

I have just replied in the email but I thought it would be good to post it on the forums as well.

I added a new hook in /oc-includes/osclass/locale.php (line 40)
Code: [Select]
osc_runHook('plugin_load_translation');
Then I edit the real estate plugin like such:
Code: [Select]
function realstate_load_translation() {
    global $gt,$locale;
    $streamer = new FileReader(__DIR__ .DIRECTORY_SEPARATOR.'translations'.DIRECTORY_SEPARATOR.$locale.'.mo');
    if (isset($gt)) {
        $gt->gettext_reader($streamer);
    }
}

osc_addHook('plugin_load_translation','realstate_load_translation');

I dont know if its the best way to go about doing this but, since cache is automatically set to true, it loads the mo file that translates the real estate plugin and appends the general translation ($gt) handler (it overwrites duplicate entries).

Regards,
e2.

_CONEJO

  • Administrator
  • Hero Member
  • *****
  • Posts: 4689
Re: Translating Plugins
« Reply #3 on: January 27, 2011, 03:04:56 pm »
Hi e2,

That was great. We implemented a derivated solution of yours, but I have a doubt, is that working for you?

Maybe I missed something, but as far as I know, plugins are loaded AFTER locale.php , so It should not find the function osc_runHook.


Our solution is this :
Code: [Select]
function osc_loadTranslation($dir = __DIR__) {

    global $gt,$locale;
    if(file_exists($dir.DIRECTORY_SEPARATOR.'translations'.DIRECTORY_SEPARATOR.$locale.'.mo')) {
        $file = $dir.DIRECTORY_SEPARATOR.'translations'.DIRECTORY_SEPARATOR.$locale.'.mo';
    } else if(file_exists($dir.DIRECTORY_SEPARATOR.'locales'.DIRECTORY_SEPARATOR.$locale.'.mo')) {
        $file = $dir.DIRECTORY_SEPARATOR.'locales'.DIRECTORY_SEPARATOR.$locale.'.mo';
    } else if(file_exists($dir.DIRECTORY_SEPARATOR.$locale.'.mo')) {
        $file = $dir.DIRECTORY_SEPARATOR.$locale.'.mo';
    } else {
        return false;
    }

    $streamer = new FileReader($file);
    if (isset($gt)) {
        $gt->gettext_reader($streamer);
    }
}

The you can call it directly after the load of the plugin :
Code: [Select]
osc_loadTranslation(__DIR__);
We think it's easier for plugin developers to include a one-liner instead of some more. Also, as I commented before, plugins should be being loaded after, I will investigate this (but I don't have a .mo file yet for plugins, so I can not test it) and create a hook for that (but I think that the osc_loadTranslation it's a easy solution.

Thanks!

e2

  • Newbie
  • *
  • Posts: 7
Re: Translating Plugins
« Reply #4 on: January 27, 2011, 05:01:27 pm »
Hi

Thats excellent you've implmented it, thanks :)

I can save you some time and tell you that plugins are loaded before locale:
Code: [Select]
require_once  LIB_PATH . 'osclass/plugins.php';
require_once  LIB_PATH . 'osclass/themes.php';
require_once  LIB_PATH . 'osclass/utils.php';
require_once  LIB_PATH . 'osclass/locale.php';

Thats oc-load.php

Your solution is better then mine though, but would not work because if osc_loadTranslation is part of loacle.php (where it should be) then, the plugin wont be able to execute it.

I am using v1.2 Delta.

Regards,
e2.

_CONEJO

  • Administrator
  • Hero Member
  • *****
  • Posts: 4689
Re: Translating Plugins
« Reply #5 on: January 27, 2011, 05:54:25 pm »
Both solutions are great, for some reason i'm not able to tell you (I just don't remember), we change the order of libraries loading in the current development release. I remember doing some changes to oc-load.php because of something not being defined in the correct order, but I can't tell you what exactly was.

I will check again and try to implement the hook which is an elegant solution too.

Thanks for all the feedback and help!

kingsult

  • Premium
  • Full Member
  • *****
  • Posts: 204
Re: Translating Plugins
« Reply #6 on: February 01, 2011, 06:21:19 pm »
Hi e2,

Feel free to share the plugins with us  :D

Juan Ramón

  • Osclass Developer
  • Hero Member
  • *****
  • Posts: 2382
Re: Translating Plugins
« Reply #7 on: February 03, 2011, 02:15:46 pm »
Hi, if you want to translate plugins and themes, you should change the code of oc-includes/osclass/locale.php for this one: https://gist.github.com/809349

An example of the code you've to put in the index.php of the plugin:
Code: [Select]
osc_loadTranslation(dirname(__FILE__), 'jobs_attributes')
The translation file should be in the root folder of the plugin (e.g: es_ES.mo or fr_FR.mo).

Finally, the string should be:
Code: [Select]
_e('Hello', 'jobs_attributes');
__('Hello', 'jobs_attributes');