2.0.x-dev
2.0.9999999.9999999-devA Bundle for switching Languages
MIT
The Requires
by instaclick
locale
A Bundle for switching Languages
This bundle is in refactoring state. You can see the vision the bundle will take by looking inside the comments of the PR #5 or looking into the Wiki., (*1)
You can follow the refactoring by looking into the refactoring
branch. The master
branch remains the last stable version for the Symfony 2.0.* framework., (*2)
This Symfony2 Bundle consists currently of 3 Parts, (*3)
The Listener uses the $request->getPreferredLanguage()
and $request->getLanguages()
methods which gather infomation about the browser language (see http://en.wikipedia.org/wiki/Content_negotiation)., (*4)
The Listener also checks against the allowed_language
list to ensure that the application locale will only be set to an allowed locale / language., (*5)
After the locale is identified, the is saved in the Session, because we only want to do the locale lookup only once per user and not on every page request., (*6)
If the user changes the language manually (with the "Switch Language" action from this bundle), the app will locked to that language., (*7)
Additionally a cookie with the user-selected language is set. (For returning visitors)., (*8)
If you have a project with registered users, you should always set the language and region provided by the user, e.g. set the application locale after login., (*9)
see: http://symfony.com/doc/2.0/book/translation.html, (*10)
If no languages are supplied from the Browser, the locale will be set to the default %locale%
defined in app/config.yml
., (*11)
If the preferred language from $request->getPreferredLanguage()
matches a language from our allowed_languages
list defined in app/config.yml
, the application locale will be set to the preferred locale., (*12)
Example: The preferred language is set to 'de_AT', the Listener will check for the primary language (which is 'de') and set it to 'de_AT', since we also want to use/set the Region of the user., (*13)
If the preferred language doesn't match any of our allowed_languages
, the provided languages from $request->getLanguages()
will be checked if theres a match to any of our allowed languages
and sets it to the first matching allowed_language
the user has set (highest priority)., (*14)
Example:, (*15)
We allow the following Languages:, (*16)
The preferred Language is set to 'ru_RU', but the Browser provides additional locales/languages:, (*17)
Then the Locale will be set to 'en_US', cause it is the first matched language. It also provides a Region setting (US) which can be used for formatting a date or use the currency for that region., (*18)
This Controller lets the User choose the language manually. It'll also check for the predefined available languages and redirects to the target specified in in the app/config.yml
switch_router:
section., (*19)
This Twig extension is forked from craue/TwigExtensionsBundle. The plugin displays the allowed languages in <li>
tags., (*20)
The options are described below in the app/config.yml
section., (*21)
There are already a few translations for the show_languagetitle
option, feel free to correct / add translations :), (*22)
[LuneticsLocaleBundle] git=http://github.com/lunetics/LocaleBundle.git target=bundles/Lunetics/LocaleBundle
(add to deps file and run ./bin/vendors install
), (*23)
Add the Bundle to your application's kernel:, (*24)
// app/AppKernel.php public function registerBundles() { $bundles = array( //... $bundles[] = new Lunetics\LocaleBundle\LuneticsLocaleBundle(); //... }
Add the namespace Lunetics to the Autoloader:, (*25)
$loader->registerNamespaces(array( //... 'Lunetics\LocaleBundle' => __DIR__.'/../vendor/bundles' //... ));
Edit the app/config.yml file and add the following:, (*26)
lunetics_locale: # Sets the Languages which are allowed allowed_languages: # Add Language 'de' (German) - de # Add Language 'en' (English) - en # Add Language 'fr' (French) - fr # Options for the Change Language Twig Extension change_language: # Show The Language in their native Language (e.g. Deutsch / English / Français ) # Default setting is set to false show_foreign_languagenames: true # Force Uppercase of the Languagenames # Default setting is set to false show_first_uppercase: true # Adds an additional block / title ( Sprache Àndern / Change Language / Changer la langue ) # Default setting is set to false show_languagetitle: true switch_router: # Redirect to referrer after changing the language # Default setting is set to true use_referrer: true # Redirect to a custom Route after changing the language. # use_referrer must be set to false redirect_to_route: "MyBundle_homepage" # Redirect to a custom url after changing the language. # use_refferer must be set to false and redirect_to_route not be set redirect_to_url: "/"
If you want to use the included Languageswitcher, you have to add this to your app/config/routing.yml
file:, (*27)
LuneticsLocaleBundle: resource: "@LuneticsLocaleBundle/Resources/config/routing.yml" prefix: /
A Bundle for switching Languages
MIT
locale