As a user, I need to see the french version of a shop when I click on the french flag, and the english version when I click on the american flag.
[X] The static message on the website must be translated.
[X] The local must be in the url.
[X] The list of products must be displayed from the database, in the two languages.
[X] The pages must look like these designs:
N.B.: We'll use Doctrine and MySQL for the exercise.
- Create a new bundle called VendorShopBundle.
- Begin with the design of the layout in
src/Vendor/ShopBundle/Resources/views/layout.html.twig
- Integrate in it some twitter bootstrap to make it presentable.
- Create your homepage (Controller
src/Vendor/ShopBundle/Controller/DefaultController.php
+ Viewsrc/Vendor/ShopBundle/Resources/views/Default/index.html
). - In your
layout
template, make that the meta description, meta keywords and title of the page are translated with those keys{{ 'meta.keywords'|trans }}
,{{ 'meta.description'|trans }}
and{{ 'shop.name'|trans }}
. - Use the tag
{% trans_default_domain 'index' %}
to use the catalogsindex.en.yml
andindex.fr.yml
. - Create the catalogs in
src/Vendor/ShopBundle/Resources/translations/index.fr.yml
andsrc/Vendor/ShopBundle/Resources/translations/index.en.yml
. - Enable translation in the
app/config/config.yml
by uncommenting the linetranslator: { fallback: "%locale%" }
under theframework
key. - Don't forget to clear you're cache, as you changed you configuration ;)
- Complete your
src/Vendor/ShopBundle/Resources/views/Default/index.html
template. - Create your product list page (Controller
src/Vendor/ShopBundle/Controller/ProductController.php
+ Viewsrc/Vendor/ShopBundle/Resources/views/Product/index.html
). - Complete your
src/Vendor/ShopBundle/Resources/views/Product/index.html
template. - Create a event susbsriber
src/Vendor/ShopBundle/EventListener/LocaleListener.php
that will be responisble of changing the locale of the request, put the locale in session and set other languages available so only right flags will be displayed to the user. - In the layout, add the snippet of code to display the other available language through flags
(mind adding the picto in
src/Vendor/ShopBundle/Resources/public/images/pictos
). - Now time to make sure that all routes of the bundle are prefixed with a locale (don't forget to add requirements and a default).
- Then, create the entities
src/Vendor/ShopBundle/Entity/Product.php
andsrc/Vendor/ShopBundle/Entity/ProductTranslation.php
(oneToMany relation). - Add the
src/Vendor/ShopBundle/Translation/TranslationProxy.php
(responsible of the translation system). - Create some fixtures (the
doctrine/doctrine-fixtures-bundle
package is needed). - Create your database, your schema and load your fixtures.
- Add the right code in the
src/Vendor/ShopBundle/Resources/views/Product/index.html
template to display your product information.
Et voilà !
Some documentation:
- http://symfony.com/doc/current/book/translation.html
- http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
- http://symfony.com/doc/current/components/translation/index.html
- http://moquet.net/talks/phptour-2014-i18n/
You can use the https://github.com/lexik/LexikTranslationBundle to manage your translation in database.
The bundle is integrated in this branch : https://github.com/poledev/Katas/tree/kata-translation
NB: AngularJS is needed. And don't forget to launch the app/console assets:install
command.
The interface is available on /translations/grid
uri.
- To import your bundle translation :
app/console lexik:translations:import VendorShopBundle
- To export your translations from the database :
app/console lexik:translations:export
NB: For the exportation, mind that you must create first the app/cache/dev/Resources/translations so the new translations created via the web interface can be dumped in the yml files.
Add the new lines in the original files, you can then, import it via the
app/console lexik:translations:import VendorShopBundle
.