简体   繁体   English

如何在树枝中使用国际化功能

[英]how to use internationalization function in twig

In my project I'd like tu use the internationalisation function in my twig vue to display a specific country names according to its locale in the app.request.locale .在我的项目中,我希望你使用我的app.request.locale vue 中的国际化功能根据app.request.locale locale显示特定的国家/地区名称。

I was thinking that twig-extention was my solution but only localizedDate , localizedCurrency and localizedNumber are available.我认为twig-extention是我的解决方案,但只有localizedDatelocalizedCurrencylocalizedNumber可用。

Is there a simple way to use this functionality ?有没有一种简单的方法来使用这个功能?

Maybe you can create an extension of your own.也许您可以创建自己的扩展。 Something like that :类似的东西:

<?php

namespace App\Twig;

use Locale;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class TranslateCountryExtension extends AbstractExtension
{
    const COUNTRY_LIST = [
        'AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ',
        'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BQ', 'BA', 'BW', 'BV', 'BR',
        'IO', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC',
        'CO', 'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 'HR', 'CU', 'CW', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO',
        'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF', 'GA',
        'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY', 'HT',
        'HM', 'VA', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT', 'JM', 'JP',
        'JE', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY', 'LI',
        'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX',
        'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'NC', 'NZ', 'NI',
        'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH', 'PN',
        'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'BL', 'SH', 'KN', 'LC', 'MF', 'PM', 'VC', 'WS',
        'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 'SG', 'SX', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'SS',
        'ES', 'LK', 'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 'TK',
        'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 'UM', 'UY', 'UZ', 'VU',
        'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW',
    ];

    public function getFunctions()
    {
        return array(
            new TwigFunction('countryList', [$this, 'countryList']),
        );
    }

    public function countryList(string $locale): array
    {
        $translatedCountries = [];

        foreach( self::COUNTRY_LIST as $countryId) {
            $translatedCountries[$countryId] = Locale::getDisplayRegion($countryId);
        }

        return $translatedCountries;
    }
}

And use it in your Twig templates like:并在您的 Twig 模板中使用它,例如:

{% for key, value in countryList(app.locale) %} etc etc ... 

I don't know about a way to display all Locale class region values so I used a constant.我不知道显示所有 Locale 类区域值的方法,所以我使用了一个常量。

Following the recommandation of emix and Yoann Mir , I created my own filter (it is more appropriate than a function) that use php locale:根据emixYoann Mir的推荐,我创建了自己的过滤器(它比函数更合适),使用 php 语言环境:

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Locale;
use Twig\TwigFilter;

class CountryExtention extends AbstractExtension
{
    private $defaultLocale;

    public function __construct($defaultLocale){
        $this->defaultLocale = $defaultLocale;
    }

    public function getFilters()
    {
        return [
            new TwigFilter('alpha2', [$this, 'getAlpha2code']),
            new TwigFilter('language', [$this, 'getLanguageName'])
        ];
    }

    /**
     * Transform a locale variable in a Alpha2 country code 
     * 
     * @param string $locale the locale to be transformed in RFC 4646
     * 
     * @return string|null the alpha2 code in ISO 639-1
     */
    public function getAlpha2code($locale){

        return Locale::getRegion($locale);
    }

    /**
     * Transform the locale variable in a language name displayed in the current locale
     * 
     * @param string $locale the locale to be transformed in RFC 4646
     * @param string|null $inLocale the target locale to display the language name
     * 
     * @return string|null the language name in the target locale or in the app.locale
     */
    public function getLanguageName($locale, $inLocale = null)
    {
        if (!$inLocale)
            $inLocale = $this->defaultLocale;

        return Locale::getDisplayLanguage($locale, $inLocale); 
    }
}

then add it to my services:然后将其添加到我的服务中:

service:
    _defaults:
        bind:
            $defaultLocale: locale

    App\Twig\CountryExtention:
        tags:
            - { name: twig.extention }

and use it in my twig as such:并在我的树枝中使用它:

{% for locale in twig_locales %}
    <a class="dropdown-item" href="#">
        <span class=" mr-1">
            <i class="flag-icon flag-icon-{{ locale|alpha2|lower }}"></i>
        </span>
        {{ locale|language(app.request.locale) }}
    </a>
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM