私はsymfonyアプリケーションに取り組んでおり、ユーザーがどのページにいてもページのロケールバージョンに移動することが目標です。
たとえば、ユーザーがホームページの「/」に移動すると、「/en/」にリダイレクトされます。
_locale
それらが「/admin」ページにある場合、プロパティがルートから設定されるように、「/en/admin」にリダイレクトされます。
また、ユーザーのブラウザから /admin にアクセスする場合はロケールを決定する必要があります。これは、ロケールが決定されていないため、リダイレクト先のページがわかるためです。
現在、テスト中のため、デフォルトのコントローラーは次のようになっています。開発モードとプロファイラーを使用して、翻訳が正しく機能していることをテストしています。
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
* @Route("/{_locale}/", name="homepage_locale")
*/
public function indexAction(Request $request)
{
$translated = $this->get('translator')->trans('Symfony is great');
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
'translated' => $translated
]);
}
}
この現在の方法では、ユーザーがそこに移動すると「/」に留まりますが、「/en/」にリダイレクトしたいと考えています。これは、/admin や /somepath/pathagain/article1 (/en/admin 、 /en/somepath/pathagain/article1) などの他のページでも機能するはずです。
どうすればいいですか?
私が読んだ参考文献は役に立ちませんでした:
Symfony2 ルーティングでデフォルトのロケールを使用する (1 つの言語に対して 1 つの URL)
ルーティングにおける Symfony2 のデフォルトロケール
::アップデート::
私は自分の問題を解決していませんが、より効率的にするためのいくつかのトリックを学びました。
DefaultController.php
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @Route("/", name="home", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
* @Route("/{_locale}/", name="home_locale", requirements={"_locale" = "%app.locales%"})
*/
public function indexAction(Request $request)
{
$translated = $this->get('translator')->trans('Symfony is great');
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
'translated' => $translated
]);
}
/**
* @Route("/admin", name="admin", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
* @Route("/{_locale}/admin", name="admin_locale", requirements={"_locale" = "%app.locales%"})
*/
public function adminAction(Request $request)
{
$translated = $this->get('translator')->trans('Symfony is great');
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
'translated' => $translated
]);
}
}
?>
構成.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
app.locales: en|es|zh
framework:
#esi: ~
translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments: ~
http_method_override: true
assets: ~
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
parameters の下の value に注意してくださいapp.locales: en|es|zh
。これは、今後さらに多くのロケールをサポートする予定がある場合に、ルートを作成するたびに参照できる値です。これらのルートは、好奇心旺盛な人のために、英語、スペイン語、中国語の順です。注釈の DefaultController では、"%app.locales%"
構成パラメーターを参照する部分です。
私の現在の方法の問題は、/ admin に行くことです。たとえば、ユーザーを /{browsers locale}/admin にリダイレクトしません。これは、すべてを整理するためのよりエレガントなソリューションになります...しかし、少なくともルートは機能します。まだより良い解決策を探しています。
****アップデート****
Athlan の回答である下の回答 (ロケールと要件をすべてのルートに追加 - Symfony2 )として、ここで回答を見つけた可能性があると思います。彼の指示が私には十分に明確ではなかったため、symfony 3 でこれを実装する方法がわかりません。
この記事も役立つと思います ( http://symfony.com/doc/current/components/event_dispatcher/introduction.html )