basz / SlmLocale

Auto detection of locale through different strategies for Zend Framework 2
Other
68 stars 34 forks source link

Translator can't recognize slm_locale language #63

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hi,

I have i little problem about using translator object.

At the layout, I have set the lang parameter like that:

<html lang="<?php echo Locale::getPrimaryLanguage(Locale::getDefault())?>">

And it work, when i looking at source i see:

<html lang="fr">

That is my slmlocale.global.php

$settings = array(
    'default' => 'fr',
    'supported' => array('fr', 'en'),
    'aliases' => array(
        'fr-FR' => 'fr',
        'en-US' => 'en',
    ),    
    'strategies' => array('uripath', 'acceptlanguage'),
);
return array(
    'slm_locale' => $settings
);

And that is an extract of my module.config.php

    'translator' => array(
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),

However when i use $this->translate("A 404 error occurred");, the string is not translated.

juriansluiman commented 9 years ago

@xiang9428 how are your translation files named?

The discrepancy between locale and language is a reoccurring problem for SlmLocale, I guess I have to document this. You have fr-FR and fr. With those two, fr-FR is the locale and fr a language. SlmLocale deals with locales, not languages.

So, you have to support fr-FR and en-US as locales, set fr-FR as your default and alias fr to fr-FR. If you have a file named language/fr-FR.mo, all should go well. The Locale::getPrimaryLanguage() will "transform" the fr-FR locale into fr as language.

ghost commented 9 years ago

My language file named like that fr_FR.mo, and if i don't want to change all my mo and po files, i have to find an other way. I don't know if my method is the best way but i try something like that in my module.config.php

$supportedLanguages = array("fr" => "fr_FR",
                            "en" => "en_US",
                            );
return array(
[...]
    'translator' => array(
        'locale' => $supportedLanguages[Locale::getDefault()],
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
[...]
);
juriansluiman commented 9 years ago

You do not need to set a 'locale' key in the configuration. This locks the locale to that specific locale, but the Locale::getDefault() is not set by SlmLocale.

If you remove the key, what happens next? If this does not solve the issue, can you dump Locale:: getDefault() in e.g. your view?

ghost commented 9 years ago

I removed the key but it doesn't work, the translator doesn't work ...

When i dump Locale:: getDefault() i have a locale language like fr, en etc.

juriansluiman commented 9 years ago

So it seems the SlmLocale is configured incorrectly. The translator doesn't show the translation because it cannot find the locale set as default. Your files are named correctly (fr_FR.mo) and there's no problem with that part. Have you read my first comment on this issue? If not, please correct your config first. Because, based on the config you posted initially, "fr" is the expected outcome and you actually need to make SlmLocale detect "fr_FR".

ghost commented 9 years ago

I changed my config, now it's like that slmlocale.global.php

$settings = array(
    'default' => 'fr_FR',
    'supported' => array('fr_FR'),
    'aliases' => array(
        'fr' => 'fr_FR',
    ),
    'strategies' => array('uripath', 'acceptlanguage'),
);
return array(
    'slm_locale' => $settings
);

In my module.config.php i deleted my manual setting 'locale' key in the configuration of translator. Everything work but when i go on my site it automatically use a link as that format http://www.website.com/fr_FR and i want to use an alias like that http://www.website.com/fr.

juriansluiman commented 9 years ago

Now the detection is working correctly, you can alias locales for specific the uripath strategy:

'strategies' => array(
    array(
        'name'    => 'uripath',
        'options' => array(
            'aliases' => array('fr' => 'fr_FR', 'en' => 'en_US'),
        ),
    ),
    'acceptlanguage',
),

This way, only the UriPath strategy has a set of aliases used for the path segment. In this case, "fr_FR" and "fr" will be detected as "fr_FR" but a redirect happens to the alias and not the complete locale.

ghost commented 9 years ago

Ow that works, thank you very much !