Symfony translation to javascript 翻譯檔動態生成 js, json

套件

安裝

  1. 用上方 composer 安裝套件
  2. 到 app/AppKernel.php 註冊 bundle
  3. 在 app/config/routing.yml 註冊 routing
  4. Publish assets,會將 vendor 內的 js link 到 asset 中

JS端使用方式

  1. require translator.min.js
    1. 若安裝流程正確,會在 web/bundles/ 底下看到
    2. 或者手動到 vendor/willdurand/js-translation-bundle/ 下,複製來使用
    <script src="{{ asset('web/bundles/bazingajstranslation/js/translator.min.js') }}"></script>
    
  2. require 字典檔
  3. <script src="{{ url('bazinga_jstranslation_js', { 'domain': 'messages', 'locales': 'zh_TW,en' }) }}"></script>
    
  4. 呼叫使用
  5. console.log(Translator.trans('aaa.bbb'));
    
  6. 完整的範例
  7. {% if app.request.locale == 'zh_TW' %}
        {% set ln = 'zh-Hant' %}
    {% elseif app.request.locale == "zh_CN" %}
        {% set ln = 'zh-Hans' %}
    {% else %}
        {% set ln = 'en' %}
    {% endif %}
    
    <html lang="{{ ln }}">
    
    <head>
    <script src="{{ asset('web/bundles/bazingajstranslation/js/translator.min.js') }}"></script>
    <script src="{{ url('bazinga_jstranslation_js', { 'domain': 'messages', 'locales': app.request.locale~',en' }) }}"></script>
    
    <script>
        Translator.locale = '{{ app.request.locale }}';
        console.log(Translator.trans('aaa.bbb'));
    <script>
    
    </head>
    

PHP端使用方式
  1. 在 bundle folder 底下的 Resources/translations 新增 messages.en.yml
  2. aaa:
        bbb:哈囉
    
  3. config.yml
  4. parameters:
        locale: en
        app.locales: en|zh_TW|zh_CN
        locale_supported: ['en','zh_TW','zh_CN']
        api_url: 'http://ems2.xmight.com/test/api/v1'
        
    framework:
        translator: { fallbacks: ['%locale%'] }
    
  5. routing.yml
  6. 
    change_lang:
        path:     /locale/{_locale}
        defaults: { _controller: AppBundle:Default:changeLocale }
        requirements: 
            _locale: '%app.locales%'
    
  7. 新增 EventListener/LocaleListener.php 到你的 bundle folder
  8. defaultLocale = $defaultLocale;
        }
    
        public function onKernelRequest(GetResponseEvent $event)
        {
            $request = $event->getRequest();
            if (!$request->hasPreviousSession()) {
                return;
            }
            
            //get From session
            $sessLang = $request->getSession()->get('_locale');
            
            if(empty($sessLang)){
                //get From cookie
                $plang = $request->cookies->get('_locale');
                
                //get From db
                
                //exists set to session
                
                //none use Per
                if(empty($plang)){
                    $plang = $request->getPreferredLanguage();
                    
                    if( ! in_array($plang, ['en','zh_TW','zh_CN'])){
                        $plang = 'en';
                    }                
                }
                
                $request->getSession()->set('_locale', $plang);
            }
            
            // try to see if the locale has been set as a _locale routing parameter
            if ($locale = $request->attributes->get('_locale')) {
                $request->getSession()->set('_locale', $locale);
            } else {
                // if no explicit locale has been set on this request, use one from the session
                $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
            }
        }
    
        public static function getSubscribedEvents()
        {
            return array(
                // must be registered after the default Locale listener
                KernelEvents::REQUEST => array(array('onKernelRequest', 15)),
            );
        }
    }
    
     //Controller
        public function changeLocaleAction(Request $request, $_locale)
        {
    
            $request->attributes->set('_locale', null);
            $request->getSession()->set('_locale', $_locale);
    
            $response = new JsonResponse(['success' => true], Response::HTTP_OK);
            $response->headers->setCookie(new Cookie('_locale', $_locale));
    
            return $response;
        }
    

Symfony translation to javascript 翻譯檔動態生成 js, json Symfony translation to javascript 翻譯檔動態生成 js, json Reviewed by Wild on 5/14/2018 04:57:00 下午 Rating: 5

沒有留言:

沒有Google帳號也可發表意見唷!

技術提供:Blogger.