Developed with love by KnpLabs Hire us for your project!
36

ServerGroveLocaleBundle

by servergrove

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>
Copyright (c) 2012 ServerGrove Networks, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
server_grove_locale:
flags_path: /home/deploy/prod/shared/app/repositories/servergrove/ServerGroveLocaleBundle/Resources/public/images
enabled_locales: []
hide_current_locale: true
cache_warmer:
enabled: true
patterns:

# Defaults:
- /^(?P<locale>[a-z]{2}).png$/
- /^(?P<locale>[a-z]{2})\-(?P<country>[A-Z]{2}).png$/
defaults:

# Prototype
lang: []
template: ServerGroveLocaleBundle::flags.html.twig
loader:
class: ServerGrove\LocaleBundle\Flag\CacheLoader
arguments:

# Default:
- %kernel.cache_dir%
domains:

# Prototype
locale: []
  • Merge pull request #21 from pborreli/typos
    By pgodel, 6 years ago
  • Fixed PHPDoc
    By pborreli, 6 years ago
  • added php 5.5
    By pgodel, 6 years ago
  • updated composer config
    By pgodel, 6 years ago
  • updated authors
    By pgodel, 6 years ago
  • Merge pull request #20 from EmmanuelVella/patch-1
    By pgodel, 6 years ago
  • Update symfony deps
    By emmanuelvella, 6 years ago
  • Update Asset/Factory/Loader/Loader.php
    By iambrosi, 6 years ago
  • Fixed configuration. Added tests for DI
    By iambrosi, 6 years ago
  • Added autoload information
    By iambrosi, 6 years ago
  • Updated dependencies
    By iambrosi, 6 years ago
  • Added license
    By iambrosi, 6 years ago
  • Merge pull request #18 from iambrosi/force-locale
    By pgodel, 7 years ago
  • Removed unused classes
    By iambrosi, 7 years ago
  • Updated composer
    By iambrosi, 7 years ago
  • Renamed bundles in test
    By iambrosi, 7 years ago
  • Added default in test case
    By iambrosi, 7 years ago
  • Added attrs test for renderPathFlag
    By iambrosi, 7 years ago
  • Fixed test for CacheWarmer
    By iambrosi, 7 years ago
  • Fixes for attributes
    By iambrosi, 7 years ago
  • Removed FlagExtensionFactory
    By iambrosi, 7 years ago
  • Added FlagTest
    By iambrosi, 7 years ago
  • Added test for forceDefault method
    By iambrosi, 7 years ago
  • Added vendor folder as excluded
    By iambrosi, 7 years ago
  • Added more control for attributes
    By iambrosi, 7 years ago
  • Test case for FlagExtension
    By iambrosi, 7 years ago
  • Asset names in lowercase
    By iambrosi, 7 years ago
  • Several changes to use AsseticBundle AssetManager
    By iambrosi, 7 years ago
  • Merge pull request #17 from iambrosi/force-locale
    By pgodel, 7 years ago
  • Fixed default rewrite
    By iambrosi, 7 years ago