book
  • Book
  • Nette Framework
    • How to start developing multilanguage website with Nette Framework and Kdyby\Translation
    • Jak na multijazyčný web s Nette Framework a Kdyby\Translation
    • Načítání Facebook postů v Nette Framework
    • Jak začít a propojit Doctrine a Nette Framework
    • Zkrášlujte formulářové prvky, snadno pomocí setOption
    • Jak řešit překlady flash zpráviček v Nette Framework
Powered by GitBook
On this page
  • Installation
  • Configuration
  • Presenter
  • Router
  • Dictionaries
  • Dictionary content
  • Template
  • Profit

Was this helpful?

  1. Nette Framework

How to start developing multilanguage website with Nette Framework and Kdyby\Translation

PreviousBookNextJak na multijazyčný web s Nette Framework a Kdyby\Translation

Last updated 4 years ago

Was this helpful?

Many people on Nette forum asked how to develop a multilingual website. This was my question as well and I'd like to share my solution with you. Filip Prochazka has developed , an adapter for Symfony\Transaltion for Nette Framework, and has lots of super truper small features which you will love.

Here is a four minute long videocast, an introduction to how easy it is to start using Kdyby\Translation.

Installation

1) Install Nette web-project: $ composer create-project nette/web-project

2) Install Kdyby/Translation: $ composer require kdyby/translation

Configuration

in config.neon we will add an extension to enable Kdyby\Translation:

extensions:
        translation: Kdyby\Translation\DI\TranslationExtension

Presenter

In our BasePresenter we will add the variables $locale and $tranlator. Using [Dependency Injection | doc:di] mechanism we get an injected Kdyby\Translation\Translator service. (In my example, I don't use any BasePresenter, instead I only use the HomepagePresenter).

The variable $locale is and will now be automatically held in the url.

{
        /** @persistent */
        public $locale;

        /** @var \Kdyby\Translation\Translator @inject */
        public $translator;
}

Router

Update the original router definition in RouterFactory.php

$router[] = new Route('[<locale=en cs|en>/]<presenter>/<action>', "Homepage:default");

(In the video, locale defaults to cs.)

Dictionaries

If we want a specific translation with localization, e.g. French speaking Canadians, we will use fr_CA.

Keeping both the language and the country in the culture is necessary because you may have a different French translation for users from France, Belgium or Canada, and a different Spanish content for users from Spain or Mexico. The language is a code of two lowercase characters, according to the ISO 639-1 standard (for instance, en for English). The country is a code of two uppercase characters, according to the ISO 3166-1 standard (for instance, GB for Great Britain).

Dictionary content

ui.cs_CZ.neon

title: Vitejte

ui.en_US.neon

title: Hello

Template

In our template, we will use the macro {_ ui.title} for putting translated phrase.

Profit

It's really easy to start.

We will add the two files ui.cs_CZ.neon and ui.en_US.neon to the folder app/lang. Here ui is the name of our dictionary and cs_CZ is a combination of language and state. ( and )

The default syntax is Neon, you can read more about its amazing syntax .

For more information read the .

Kdyby\Translation
https://www.youtube.com/watch?v=FFOO5j-zpSU
persistent
ISO_639-1
ISO 3166-1
http://symfony.com/legacy/doc/book/1_0/en/13-I18n-and-L10n
http://ne-on.org
official documentation for Kdyby\Translation