Jump to content

Module with many include files always default language


Recommended Posts

Im building a module with many (25+) include files. Now i came up with the idea to have a central include file which contains all the language values so that i have one file that can be added for translation (instead of 25+).

I have added (besides the default language) the following nl=Nederlands and de=Detusch languages.

For testing i modified the guest user and set its language to de. The problem is when i look at the page that should have translated values it renders the default language only.

Probably its the way i have set it up, but i can't think of another way to do it. I want to keep having only 1 central language file that i include once.

The set up is somewhat like this:

MyModule.module

class MyModule extends WireData implements Module, ConfigurableModule {

    /**
     * Returns language array
     *
     * @return array
     */
    private $lang = array();

    /**
     * Returns gender type
     *
     * @static
     * @return array
     */
    private static $gender = array();

    /**
     *
     * Initialize the module
     */
    public function init() {

        // Include all language variables
        @include_once (dirname(__FILE__) . '/language.inc');

        self::$gender = array(
            1 => $this->lang['Male'], // Gender
            2 => $this->lang['Female'], // Gender
        );
    }

    /**
     * Render Male
     *
     * @return string
     */
    public function ___renderMale() {
        return $this->lang['Male'];
    }
}

language.inc

/**
 * Gender
 */
$this->lang['Male'] = $this->_('Male'); // Gender
$this->lang['Female'] = $this->_('Female'); // Gender

And in my template file:

$mymod = $modules->get('MyModule');

echo $user->name;
echo "<br>";
echo $user->language->name;
echo "<br>";
echo $mymod->renderMale();

This renders

guest
de
Male

While i would expect

guest
de
Mann

See the strange part is the current user is guest with de as selected language but it does not echo the german value of Male.

The module consist of the following files:

post-694-0-70017000-1400596424_thumb.png

  • Like 1
Link to comment
Share on other sites

@Raymond: just a thought: have you tried to "        // Include all language variables         @include_once (dirname(__FILE__) . '/language.inc');" in function ready() instead init()?

Edited by horst
  • Like 1
Link to comment
Share on other sites

@horst thanks for the suggestion but it didnt make any difference. 

edit: It somehow looks like the translation strings are bound to the file and not accessable from somewhere else. Since when i move the statement to the main module file itself it works as expected.

I might need to look in to Textdomains as described in the documentation :)

Edited by Raymond Geerts
Link to comment
Share on other sites

You have to use textdomains. The translations strings are parsed statically from the source code and are bound to a file.

From the docs:

echo __('Save', '/site/templates/common.php');

Cheers

  • Like 3
Link to comment
Share on other sites

@Martijn: Thanks that solved it! Altough its a bit confising to me because the file with the translation strings is directly included in the init() method of the module class. That made me think that it is inside the module scope.

@Wanze: Just before martijn posted his solution i tried that (with $this->_('Save', $textdomain)), but i got a warning message saying "That file has no translatable phrases". Perhaps it will do fine with __('Save', $textdomain)

I tried this (which gave the warning)

$textdomain = ltrim ($this->config->urls->siteModules . 'MyModule/MyModule.module', '/');
/**
 * Gender
 */
$this->lang['Male'] = $this->_('Male', $textdomain); // Gender
$this->lang['Female'] = $this->_('Female', $textdomain); // Gender

Thanks guys for your help

Link to comment
Share on other sites

There's couple things to consider.

Using __("Male") in the inc will work as the include itself is translated "before?" it's included, so if you translate the .inc the textdomain is the inc not the module.

Second if you include the inc in the init you'll get the language the user has set in admin! On the front-end he would always have that language regardless of what language he is. SO you have to include the translation in the ready() as suggested by horst, this will make it respect the current language you are on the website.

This is because the language is determined by the url requested ( /de/, /en/) and this is done via LanguageSupportPageNames. This module hooks into many things to accomplish this... and the language is determined in the ready(). So it's not possible to get a front-end language the current user is in the init() of a module. That's why you'll always get the user language (as set in admin) if you include it in the init(), no matter what front-end language he is on.

Link to comment
Share on other sites

@Martijn: Thanks that solved it! Altough its a bit confising to me because the file with the translation strings is directly included in the init() method of the module class. That made me think that it is inside the module scope.

@Wanze: Just before martijn posted his solution i tried that (with $this->_('Save', $textdomain)), but i got a warning message saying "That file has no translatable phrases". Perhaps it will do fine with __('Save', $textdomain)

I tried this (which gave the warning)

$textdomain = ltrim ($this->config->urls->siteModules . 'MyModule/MyModule.module', '/');
/**
 * Gender
 */
$this->lang['Male'] = $this->_('Male', $textdomain); // Gender
$this->lang['Female'] = $this->_('Female', $textdomain); // Gender

Thanks guys for your help

This won't work anyway.

You can't set the textdomain in a include to be the one it is included from. Usually you would only do this to get a translation from the module inside the included file using its textdomain as context. So if you would have $this->_("Male") already defined in your module you could get the translation without translating the include from the module.

Just use __("Male") and you're fine.

  • Like 1
Link to comment
Share on other sites

@soma, thanks for clearing that up. I'll stick with __("Phrase") way of setting the language strings and moved it from init() to ready(). Looks like everything works fine now.

Since the module is intended to be a Frontend User Profile system users will be able to select a language that will be saved to the user profile (user template).

As for a language switch for the guest user (at login and signup) i still need to figure out how i will set that up, but might just be possible with language switch, cookies or session and setting it for the guest user like this:

if ($this->session->lang && $this->session->lang != 'default') {
$this->user->language = $this->languages->get($this->session->lang);
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...