1

In Admin area, I can´t find the location language (code) of my country(Angola)

enter image description here

If I choose Portuguese→portuguese.

On html Lang=¨¨ will be like this pt_PT, but what I want is → pt_AO

Where can I add manually my country code language?

I am having only one store at the moment.

1 Answer 1

2

As far as I know, you can't add your country code manually. Below I show you how to do that through code.

But first, you need to know that Magento natively allow only few existing locales, listed here : vendor/magento/framework/Locale/Config.php in $_allowedLocales.

So if you need to add your country locale code, you need to add it to the list. Below I show you how I create a new variable with my custom locales list :

<?php
namespace Your\Module\Locale;

class Config extends \Magento\Framework\Locale\Config {

    /**
     * @var array Locales not define natively in the Locale Config of Magento. Uncomment a locale to enable it.
     * This list is the complete list supported in magento since you can't add locale not returned by
     * \ResourceBundle::getLocales('').
     * @see \Magento\Framework\Locale\Config::$_allowedLocales
     * @see \Magento\Framework\Locale\TranslatedLists::_getOptionLocales
     */
    protected $_customLocales = [
        'el_CY', /*Greek(Cyprus)*/
        'fr_CH', /*French(Switzerland)*/
        'sr_Latn_BA', /*Serbian(Bosnia & Herzegovina)*/
        'sr_Latn_ME', /*Serbian(Montenegro)*/
        'sr_Latn_RS', /*Serbian(Serbia)*/
    ];

    public function __construct(array $data = [])
    {
        // Get native list of locales
        parent::__construct($data);

        // Add custom locales
        if ($this->_customLocales && is_array($this->_customLocales)) {
            $this->_allowedLocales = array_merge($this->_allowedLocales, $this->_customLocales);
        }
    }
}

So in $_customLocales I list locales I want add. In constructor I merge this custom list with the native one provided by Magento. Note that my class herite from the class we discuss above.

Now we have to explain Magento to use my class instead of the native one. That take place in your module di.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\Locale\ConfigInterface" type="Your\Module\Locale\Config" />
</config>

Run a little magento setup:upgrade and now you can find your custom locales in the BO.

A last thing... This works for all existing locales, but you can't add invented ones. For example, you can't add an English from Germany ('en_DE'), that won't works. It is because Magento go through the locales list according to the PHP available locales :

// \Magento\Framework\Locale\TranslatedLists::_getOptionLocales
...
$locales = \ResourceBundle::getLocales('') ?: [];
...
foreach ($locales as $locale) {
if (!in_array($locale, $allowedLocales)) {
    continue;
}
...

With some works, we could make it working in an other way and be able to add our 'en_DE' locale.

Hope that helps !

1
  • it's working now very well. Thanks Nicolas
    – Ralfe
    Oct 22, 2017 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.