1

Topic: Translation helpers and Multiple Language plugin

Hi,

I'm working on this subject for some time, and have one question about it, how can I use i18n helper with multi lang plugin for easy translation of my pages content when some parts of page are in snippets?

Multi_lang plugin configuration is:
- STYLE - translation as page copy
- LANGUAGE SOURCE - URI

Thanks.

Thumbs up

2

Re: Translation helpers and Multiple Language plugin

hi iwan,

The i18n helper is not intended to perform automated translations if that's what you mean. Neither is the multi lang plugin.

The i18n helper provides some services (mainly for php code) to allow for translatable strings in your php code.

The multi lang plugin allows for automatic display of alternative content based on language settings. If you use the settings as you describe, then the effect would be:

- The multi lang plugin looks at the uri to determine what language the user wants instead of the page's default language.

- You would (for example) create the following page hierarchy structure:

Home Page
    |- My Page
    |- nl
        |- My Page

The "/nl/My Page" page would be the Dutch translation of "/My Page". You still have to manually create the translated content though. Maybe(!) I'll add an option to do automated translation through Google Translate in the future.

Simply click on the plugin's name in the plugin list in the Wolf CMS administration backend to view detailed information online.

Wolf CMS founder and lead developer
Please always check the Support forums and Wiki before asking. (My Ohloh account.)
Like Wolf CMS? Consider making a financial contribution or see our financial report first.

3

Re: Translation helpers and Multiple Language plugin

Hi mvdkleijn,

So I have some suggestions about improvements in WolfCMS core.

I see something like this:

1. update "use_helper" function to use minimal configuration options:

function use_helper() {
    static $_helpers = array();

    $helpers = func_get_args();

    foreach ($helpers as $helper) {
        if (is_array($helper)) { // check for array with helper name and lang config
          $helper = $helper[0];
          $helper_cfg = $helper[1];
        }
        if (in_array($helper, $_helpers)) continue;

        $helper_file = HELPER_PATH.DIRECTORY_SEPARATOR.$helper.'.php';

        if ( ! file_exists($helper_file)) {
            throw new Exception("Helper file '{$helper}' not found!");
        }

        include $helper_file;
        $_helpers[] = $helper;
        if (isset($helper_cfg)) { // if lang config is set, load it
          $helper->setLocale($helper_cfg); // As of PHP 5.3.0, it's possible to reference the class using a variable.
          unset($helper_cfg);
        }
    }
}

Example usage is now:
  use_helper(array('I18n', 'pl'));

but we still have old functionality:
  use_helper('I18n');

2. I've made some functions to detect witch language is set in the URL, one is very usefull here:

function getMultiLangBase($url, $prefix = "/", $suffix = "/") {
  $url = cutstr($url);
  $tmp = explode('/', $url, 2);
  if (array_key_exists($tmp[0], SettingController::$iso_639_1)) return $prefix.$tmp[0].$suffix;
  return $prefix;
}

it uses "cutstr" function defined like this:

function cutstr($value) {
  $pos = strpos($value, BASE_URL);
  if ($pos !== false && $pos >= 0) {
    $value = substr(substr($value, strlen(BASE_URL)), 0, (URL_SUFFIX != '' ? -strlen(URL_SUFFIX) : strlen($value)-strlen(BASE_URL)));
  } else {
    $pos = ((URL_SUFFIX != '') ? strpos($value, URL_SUFFIX) : false);
    if ($pos !== false && $pos > 0) $value = substr($value, 0, -$pos);
  }
  return $value;
}

I've added it to the end of index.php file of multi_lang plugin.

Usage of getMultiLangBase function is very simple, it returns base of language we use.
Example:

$base = getMultiLangBase(CURRENT_URI);

If URL was "/en/Home Page", $base is "/en/".
if URL was "/Home Page", $base  is "/".

All this can be used together, like this:

  $base = getMultiLangBase(CURRENT_URI, '', ''); // we don't need prefix and suffix
  if ($base == '') $base = 'pl'; // set default language for Your site
  use_helper(array('I18n', $base));

Now, we can use helpers in our snippets.
Simple and beautiful solution.

What You think?

Edit:

          $helper::setLocale($helper_cfg); // misspelled bug
// changed to
          $helper->setLocale($helper_cfg); // As of PHP 5.3.0, it's possible to reference the class using a variable.

Last edited by iwan (2011-04-27 00:02)

Thumbs up

4

Re: Translation helpers and Multiple Language plugin

I think you are trying to replicate the functionality of the multi lang plugin.

I also think that enhancement requests belong in the issue tracker, not here. Otherwise I can't keep track of them. smile

My assumption is that you want to be able to use the core's __() function to translate frontend generic text like error messages or generic text?

Using the i18n helper functionality is not a suitable solution for translating entire pages.

Wolf CMS founder and lead developer
Please always check the Support forums and Wiki before asking. (My Ohloh account.)
Like Wolf CMS? Consider making a financial contribution or see our financial report first.

5

Re: Translation helpers and Multiple Language plugin

Perhaps that Google Translate plugin could have been some help here, but all the links appear to be dead. sad

Using Wolf CMS professionally and for profit? Please consider supporting Wolf financially. Thanks!

6

Re: Translation helpers and Multiple Language plugin

mvdkleijn wrote:

I think you are trying to replicate the functionality of the multi lang plugin.

Not exactly replicate, but I've done some improvements to the code, so now I can use it with snippets, where some texts are hardcoded, and I don't want to make separate translations of each one of them for my every page, where I use snippets.

mvdkleijn wrote:

I also think that enhancement requests belong in the issue tracker, not here. Otherwise I can't keep track of them. smile

Sorry, I wanted to fit into a specific topic.

mvdkleijn wrote:

My assumption is that you want to be able to use the core's __() function to translate frontend generic text like error messages or generic text?

Exactly.

mvdkleijn wrote:

Using the i18n helper functionality is not a suitable solution for translating entire pages.

Not entire pages, only some texts in snippets, like error messages or other informations for users.

Thumbs up

7

Re: Translation helpers and Multiple Language plugin

David wrote:

Perhaps that Google Translate plugin could have been some help here, but all the links appear to be dead. sad

Maybe, but for me it's not what I want to achieve.
I need translations independent from external sources, such as google.
Sometimes they are not reliable or don't do the work right.

If I hardcode the translations for my snippets with __() function, I get exactly what I want, where it's needed.

Thumbs up

8

Re: Translation helpers and Multiple Language plugin

As described at the issue tracker page, I first ask a queston here at the forum.

It's not yet a enhancement requests, but for now only a discussion about it.
I wanted to know, what is Your opinion to this subject.

Thumbs up

9

Re: Translation helpers and Multiple Language plugin

@iwan - in you solution, when you use the __() function in a snippet, where do you store the translation?

As for the solution itself, I wouldn't go this route. I'm more inclined to put the functionality in either the Multiple Languages plugin or in the I18N helper. Certainly not in the use_helper function. Also, the Administration->Settings->Language setting in the backend is intended to provide the base language for the site.

However, now that you use case is more clear I agree that it would be useful to be able to use the __() function in Snippets. I'll take a look, based on your comments here and on the issue, at what would be the best implementation.

Wolf CMS founder and lead developer
Please always check the Support forums and Wiki before asking. (My Ohloh account.)
Like Wolf CMS? Consider making a financial contribution or see our financial report first.

10

Re: Translation helpers and Multiple Language plugin

mvdkleijn wrote:

@iwan - in you solution, when you use the __() function in a snippet, where do you store the translation?

I have not yet tested this, but my guess is this would be in main directory, between public and wolf subdirs, in i18n directory.

mvdkleijn wrote:

However, now that you use case is more clear I agree that it would be useful to be able to use the __() function in Snippets. I'll take a look, based on your comments here and on the issue, at what would be the best implementation.

Thanks, there is no need to hurry with the resolution of this issue, but if You manage to do something about that, I will be grateful.
Some nice solution would be good.

Thumbs up

11

Re: Translation helpers and Multiple Language plugin

Hi All!

I have some suggestion or improvements to the language Plugin: In the whole, the plugin ha indeed a good beginning, but: Why does this plugin refer to the page-slugs, which have to be just the same to make the whole language thingie work)? I'm asking this, because it's a BIIIIIIIG disadvantage when it comes to SEO (Search Engine Optimization). It's nearly useless to seo pages with a foreign language URI / page slug.

My suggestion would be, that the language plugin has to have some Dropdown box or select-thingie, where you can select the refering language-page belonging to the specific page, kind we have in the "related pages" plugin. Then, there has to be a way in the plugin to save the ID of that page.

Please tell me your thoughts on this one!

Dirk

Edit: ANYONE?

Last edited by dirkdigweed (2011-09-30 21:52)

Thumbs up

12

Re: Translation helpers and Multiple Language plugin

@dirkdigweed:

Sorry for so long silence, but nobody's perfect.
You maybe right with this SEO.
Marging with "related pages" plugin maybe a solution, this is what I'm thinking about.
One question how to do this?

Thumbs up

13

Re: Translation helpers and Multiple Language plugin

Another thing, I have answer to

mvdkleijn wrote:

@iwan - in you solution, when you use the __() function in a snippet, where do you store the translation?

It will be in snippet.
I assume couple of "must do" things.

1. I presented a modification of function use_helper(); (resides in Framework.php). Now it was redesigned once again, now looks like this:

function use_helper() {
    $helpers = func_get_args();
    
    foreach ($helpers as $helper) {
        if (is_array($helper)) { // check for array with helper name and lang config
            list($helper, $helper_cfg) = $helper;
        }

        $helper_file = HELPER_PATH.DIRECTORY_SEPARATOR.$helper.'.php';

        if (!file_exists($helper_file)) {
            throw new Exception("Helper file '{$helper}' not found!");
        }

        include_once $helper_file;
        if (isset($helper_cfg)) { // if lang config is set, load it
            $snippet = Snippet::findByName($helper_cfg.'-messages');
            if ($snippet !== false)
                $snippet = true;
            $helper::setLocale($helper_cfg, !$snippet);
            unset($helper_cfg);
        }
    }
}

2. Redesign of I18n helper was STRONGLY needed. Code change flows like this:

// we need to tell that we want language definition array from external source or from snippet
    public static function setLocale($locale, $external = true) { 
        self::$locale = $locale;
        if ($locale != DEFAULT_LOCALE)
            self::loadArray($external);
    }

// and second function

    public static function loadArray($external = true) {
        $catalog_file = I18N_PATH.DIRECTORY_SEPARATOR.self::$locale.'-message.php';

        // assign returned value of catalog file
        // file return a array (source => traduction)
        if (file_exists($catalog_file) && $external) {
            $array = include $catalog_file;
            self::add($array);
        } else { // internal source is snippet, its name must be in form xx-messages, where xx is translation from english to what you need, eg.: pl-messages
            $snippet = Snippet::findByName(self::$locale.'-messages');
            if ($snippet !== false) {
                // here the snippet content is evaluated, it must have php inside
                // example:
                // <?php
                // return array(
                //   'english key' => 'translated value'
                // );
                // ?>
                $array = eval('?>'.$snippet->content_html); 
                if (!is_array($array)) // or throw some error
                    throw new Exception("I18n helper definition array not set!");
            } else 
                $array = include $catalog_file;
            self::add($array);
        }
    }

3. Soon I propose a change to "issue tracker", so maybe this will be available in next update.

And MOST IMPORTED thing is that I have tested this solution, and it simply works.

See you, next time - Iwan a.k.a gmnevton.

Last edited by iwan (2012-03-19 21:57)

Thumbs up

14

Re: Translation helpers and Multiple Language plugin

@iwan - please don't post suggestions for code changes to the core code in the forum... that's why we have an issue tracker and why you can create pull requests in github

Wolf CMS founder and lead developer
Please always check the Support forums and Wiki before asking. (My Ohloh account.)
Like Wolf CMS? Consider making a financial contribution or see our financial report first.