ServerGroveLocaleBundle
This bundle provides a set of Twig functions to display browser culture codes., (*1)
, (*2)
Installation
You need to follow the steps according to your Symfony version., (*3)
Specifics to Symfony 2.0
Deps
First you need to add the bundle to your deps file, (*4)
[ServerGroveLocaleBundle]
git=https://github.com/servergrove/ServerGroveLocaleBundle.git
target=bundles/ServerGrove/LocaleBundle
and then, run the vendors script to download the bundle source, (*5)
``` bash
$ php ./bin/vendors install, (*6)
#### Autoload
The app must know where to look for our bundle classes. Adding the following line to the autoload file will do it.
``` php
<?php
// app/autoload.php
$loader->registerNamespaces(array(
// ...
'ServerGrove' => __DIR__.'/../vendor/bundles',
));
Specifics to Symfony 2.1
{
"require": {
"servergrove/ServerGroveLocaleBundle": "dev-master"
}
}
Enable the bundle
Now, we need to tell our application kernel to enable the bundle. For this we need to add a bundle instance to the kernel bundles., (*7)
``` php
<?php
// app/AppKernel.php, (*8)
public function registerBundles()
{
$bundles = array(
// ...
new ServerGrove\LocaleBundle\ServerGroveLocaleBundle(),
);
}, (*9)
Configuration
-------------
Use the default settings:
```yaml
server_grove_locale: ~
Or configure it according to your requirements:, (*10)
server_grove_locale:
# The path where to look for flag images
flags_path: "/path/to/flags" # Default: /path/to/ServerGroveLocaleBundle/Resources/public/images
# Whether should or shouldn't be displayed the active flag
hide_current_locale: true # Default: true
# Cache warmer options
cache_warmer:
enabled: true # Default: true
patterns: [ "/^(?P<locale>[a-z]{2}).png$/" ] # Default: [ "/^(?P<locale>[a-z]{2}).png$/", "/^(?P<locale>[a-z]{2})\-(?P<country>[A-Z]{2}).png$/" ]
defaults: # Default: []
en: "en-UK.png"
# Twig template with functions
template: "AcmeDemoBundle::template.html.twig" # Default: ServerGroveLocaleBundle::flags.html.twig
# Flags loader
loader:
class: "My\Loader\Class" # Default: ServerGrove\LocaleBundle\Flag\CacheLoader
arguments: [] # Default: [ "%kernel.cache_dir%" ]
# Use different domains for different locales
domains:
- { locale: "en", domain: "example.com", default: true }
- { locale: "es", domain: "example.es" }
# Set which flags should be displayed
enabled_locales: [ "en", "es*" ]
Displaying one flag
There are three helpful functions for displaying a single flag., (*11)
The flag function
The flag function displays only the flag image:, (*12)
{{ flag(locale) }}
{{ flag(locale, country) }}
The result would be:, (*13)
<img src="/images/locale/flags-en.png"/>
You can also use a third param with options., (*14)
Attributes
One of the available options is attrs, which allows to add attributes to the image., (*15)
{{ flag(locale, country, {
attrs: {
alt: "My locale flag",
title: "The title of my flag"
}
}) }}
If you set an array for a specific attribute, you would have to specify for which locale the attribute is., (*16)
{{ flag(locale, country, {
attrs: {
alt: {
en: "My locale flag in English",
},
title: "The title of my flag"
}
}) }}
The path_flag function
This bundle provides a function to display the flags with a link to a specific route., (*17)
{{ path_flag(route, locale) }}
{{ path_flag(route, locale, route_params, country, options) }}
The result would be:, (*18)
<a href="/page/en"><img src="/images/locale/flags-en.png"/></a>
The domain_flag function
It also provides a function to link the flag to a configured domain, (*19)
{{ domain_flag(locale) }}
{{ domain_flag(locale, country, options) }}
The result would be:, (*20)
<a href="http://example.com"><img src="/images/locale/flags-en.png"/></a>
With this section, you will be able to display multiple flags with just one function., (*21)
The flags function
The flags function is the equivalent for the flag function for multiple images., (*22)
{{ flags() }}
{{ flags(options) }}
The result would be:, (*23)
<img src="/images/locale/flags-en.png"/>
<img src="/images/locale/flags-es.png"/>
The path_flags function
The path_flags function is the equivalent for the path_flag function for multiple images. The _locale param for the route is set automatically., (*24)
{{ path_flags(route) }}
{{ path_flags(route, route_params, options) }}
The result would be:, (*25)
<a href="/page/en"><img src="/images/locale/flags-en.png"/></a>
<a href="/page/es"><img src="/images/locale/flags-es.png"/></a>
The domains_flags function
The domains_flags function is the equivalent for the domains_flags function for multiple images., (*26)
{{ domains_flags() }}
{{ domains_flags(options) }}
The result would be:, (*27)
<a href="http://example.com"><img src="/images/locale/flags-en.png"/></a>
<a href="http://example.es"><img src="/images/locale/flags-es.png"/></a>