问题描述
我正在尝试使用Silex框架在Web应用程序中实现翻译.所以,我想出了这个
I'm trying to implement translation in my web application with silex framework. So, i've come up with this
<?php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/../views',
));
$app->register(new Silex\Provider\TranslationServiceProvider(array(
'locale_fallbacks' => array('hr'),
)));
$app['translator'] = $app->share($app->extend('translator', function($translator) {
$translator->addLoader('xlf', new \Symfony\Component\Translation\Loader\XliffFileLoader());
$translator->addResource('xlf', __DIR__.'/../locales/hr.xlf', 'hr');
$translator->addResource('xlf', __DIR__.'/../locales/en.xlf', 'en');
$translator->addResource('xlf', __DIR__.'/../locales/sl.xlf', 'sl');
return $translator;
}));
$app->get('/', function () use ($app) {
$app['translator']->setLocale('hr');
return $app['twig']->render('home.twig', array('d' => $app['translator']->getLocale()));
});
$app->get('/{_locale}/', function() use ($app) {
$app['translator']->setLocale($app['request']->get('locale'));
return $app['twig']->render('home.twig', array('d' => $app['translator']->getLocale()));
});
$app->run();
基本上,我希望我的主页(mysite.com)默认为hr语言环境,但我无法使其正常工作.翻译工作正常,但是当我在树枝模板中检查语言环境时,我会收到"en"(我需要进行检查以输出一些额外的文本,具体取决于语言环境).如果我像mysite.com/hr或mysite.com/en那样明确输入语言环境,twig将按预期注册语言环境.
Basically, i'd like my homepage (mysite.com) default to hr locale but i can't get it to work. Translations are working properly but when i check for locale in my twig template i get 'en' (I need that check to output some extra text depending on locale). If i enter locale explicitly like mysite.com/hr or mysite.com/en twig registers locale as expected.
此外,我想知道在主页上没有指定语言环境的多语言页面是否是一种好习惯.
Also, I'm wondering if it's good practice having multilingual page with no specified locale on homepage.
推荐答案
尝试
$app['locale'] = 'hr';
Silex的默认语言环境设置为'en'.
Silex default locale is set to 'en'.
这篇关于silex和Twig本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!