Symfony translation to javascript 翻譯檔動態生成 js, json
套件
- github: https://github.com/willdurand/BazingaJsTranslationBundle
- composer: composer require willdurand/js-translation-bundle
安裝
- 用上方 composer 安裝套件
- 到 app/AppKernel.php 註冊 bundle
- 在 app/config/routing.yml 註冊 routing
- Publish assets,會將 vendor 內的 js link 到 asset 中
JS端使用方式
- require translator.min.js
- 若安裝流程正確,會在 web/bundles/ 底下看到
- 或者手動到 vendor/willdurand/js-translation-bundle/ 下,複製來使用
- require 字典檔
- 呼叫使用
- 完整的範例
<script src="{{ asset('web/bundles/bazingajstranslation/js/translator.min.js') }}"></script>
<script src="{{ url('bazinga_jstranslation_js', { 'domain': 'messages', 'locales': 'zh_TW,en' }) }}"></script>
console.log(Translator.trans('aaa.bbb'));
{% 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端使用方式
- 在 bundle folder 底下的 Resources/translations 新增 messages.en.yml
- config.yml
- routing.yml
- 新增 EventListener/LocaleListener.php 到你的 bundle folder
aaa:
bbb:哈囉
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%'] }
change_lang:
path: /locale/{_locale}
defaults: { _controller: AppBundle:Default:changeLocale }
requirements:
_locale: '%app.locales%'
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
Reviewed by Wild
on
5/14/2018 04:57:00 下午
Rating:
沒有留言:
沒有Google帳號也可發表意見唷!