Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 245 lines (172 sloc) 5.497 kb

ServerGroveLocaleBundle

This bundle provides a set of Twig functions to display browser culture codes.

Build Status

Installation

You need to follow the steps according to your Symfony version.

Specifics to Symfony 2.0

Deps

First you need to add the bundle to your deps file

[ServerGroveLocaleBundle]
    git=https://github.com/servergrove/ServerGroveLocaleBundle.git
    target=bundles/ServerGrove/LocaleBundle

and then, run the vendors script to download the bundle source

$ php ./bin/vendors install

Autoload

The app must know where to look for our bundle classes. Adding the following line to the autoload file will do it.

<?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.

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new ServerGrove\LocaleBundle\ServerGroveLocaleBundle(),
    );
}

Configuration

Use the default settings:

server_grove_locale: ~

Or configure it according to your requirements:

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.

The flag function

The flag function displays only the flag image:

{{ flag(locale) }}
{{ flag(locale, country) }}

The result would be:

<img src="/images/locale/flags-en.png"/>

You can also use a third param with options.

Attributes

One of the available options is attrs, which allows to add attributes to the image.

{{ 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.

{{ 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.

{{ path_flag(route, locale) }}
{{ path_flag(route, locale, route_params, country, options) }}

The result would be:

<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

{{ domain_flag(locale) }}
{{ domain_flag(locale, country, options) }}

The result would be:

<a href="http://example.com"><img src="/images/locale/flags-en.png"/></a>

Displaying multiple configured flags

With this section, you will be able to display multiple flags with just one function.

The flags function

The flags function is the equivalent for the flag function for multiple images.

{{ flags() }}
{{ flags(options) }}

The result would be:

<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.

{{ path_flags(route) }}
{{ path_flags(route, route_params, options) }}

The result would be:

<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.

{{ domains_flags() }}
{{ domains_flags(options) }}

The result would be:

<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>
Something went wrong with that request. Please try again.