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

LswGettextTranslationBundle

by LeaseWeb

Symfony2 bundle that adds native (faster) gettext translation support and is easy to use.

LswGettextTranslationBundle

The LswGettextTranslationBundle adds gettext translation support to your Symfony2
application. It is aimed to be faster and more user-friendly than the built-in
translation support of Symfony2.

Requirements

  • PHP with gettext support
  • Symfony 2.8+

Installation

Installation is broken down in the following steps:

  1. Download LswGettextTranslationBundle using composer
  2. Enable the Bundle
  3. Install the needed locales
  4. Set the language in your application

Step 1: Download LswGettextTranslationBundle using composer

Add LswGettextTranslationBundle in your composer.json:

{
    "require": {
        "leaseweb/gettext-translation-bundle": "*",
        ...
    }
}

Now tell composer to download the bundle by running the command:

$ php composer.phar update leaseweb/gettext-translation-bundle

Composer will install the bundle to your project's vendor/leaseweb directory.

Step 2: Enable the bundle

Enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Lsw\GettextTranslationBundle\LswGettextTranslationBundle(),
    );
}

Step 3: Install the needed locales

As described here you can list the locales
that you have installed on your (Linux) system with the command 'locale -a'. If you want
to support Dutch, English and German you should execute the following commands:

sudo locale-gen nl_NL.UTF-8
sudo locale-gen en_US.UTF-8
sudo locale-gen de_DE.UTF-8

More language codes can be found here

Step 4: Set the language in your application

Use the standard $request->setLocale('en'); to set the locale in your application.

Edit the following file to define 2 letter shortcuts for the locales (this is recommended):

Lsw/GettextTranslationBundle/Resources/config/config.yml

Usage

Usage is broken down in the following steps:

  1. Use gettext (convenience) functions in your code
  2. Extract the strings from a bundle that need to translated by gettext (.pot file)
  3. (First time only) Initialize the languages you want to support in the bundle (.po file)
  4. (Skip first time) Update the language (.po) files with the new gettext template (.pot) file
  5. Translate the language files using the excellent Poedit application
  6. Combine all translations into one file (.mo file)

Step 1: Use gettext (convenience) functions in your code

You can use the following functions:

  • _($text) Shortcut for gettext
  • _n($textSingular,$textPlural,$n) Shortcut for ngettext
  • __($format,$args,...) Shortcut for sprintf(_($format),$args,...))
  • __n($formatSingular,$formatPlural,$n,$args,...) Shortcut for sprintf(_n($formatSingular,$formatPlural,$n),$args,...))

Step 2: Extract the strings from a bundle

Use the ./app/console gettext:bundle:extract command to search a bundle for translation
strings and to store them into a gettext template (.pot) file.

Step 3: (First time only) Initialize the languages you want to support

Use the ./app/console gettext:bundle:initialize command to copy the gettext template (.pot)
file into the language specific (.po) files.

Step 4: (Skip first time) Update the gettext language (.po) files with the template

Use the Poedit application to load a gettext language (.po) file. Choose the
"Update from template" option and point Poedit to the generated gettext template (.pot) file. Review
and confirm the changes.

Step 5: Translate the language files using the excellent Poedit application

Use the Poedit application to load a gettext language (.po) file.
Translate all missing strings (shown in blue) and check and correct all fuzzy translated strings
(shown in yellow).

Step 6: Combine all translation

Use the ./app/console gettext:combine command combine all gettext language (.po) files into one
compiled gettext (.mo) file.

Copyright (c) LeaseWeb Technologies B.V.

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.
  • Merge pull request #15 from GijsL/master
    By web-flow, 2 years ago
  • Merge pull request #1 from GijsL/fix/symfony3
    By web-flow, 2 years ago
  • Replaced setStdin with setInput for Process. Bumped requirements for symfony/process.
    By GijsL, 2 years ago
  • Merge pull request #10 from marc-casoliva/patch-1
    By web-flow, 3 years ago
  • Remove deprecation messages for unquoted vars
    By web-flow, 3 years ago
  • Update README.md
    By mevdschee, 4 years ago
  • Update README.md
    By mevdschee, 4 years ago
  • Merge pull request #9 from GijsL/master
    By mevdschee, 4 years ago
  • Removed deprecations for Symfony 2.8
    By GijsL, 4 years ago
  • Update LICENSE
    By codeslash, 5 years ago
  • Merge pull request #5 from trasher/master
    By Maurits van der Schee, 6 years ago
  • Translate overrided templates; fixes #4
    By trasher, 6 years ago
  • Update composer.json
    By Maurits van der Schee, 7 years ago
  • Updated documentation to reflect new tags
    By Maurits van der Schee, 7 years ago
  • Update services.yml
    By Maurits van der Schee, 7 years ago
  • Update GettextTranslationExtension.php
    By Maurits van der Schee, 7 years ago
  • Update README.md
    By Maurits van der Schee, 7 years ago
  • Update composer.json
    By Maurits van der Schee, 7 years ago
  • Update LICENSE
    By Maurits van der Schee, 7 years ago
  • Merge remote-tracking branch 'Minras/master'
    By Maurits van der Schee, 7 years ago
  • Update Command/AbstractCommand.php
    By LeaseWeb Labs, 7 years ago
  • Applied Symfony2 coding standard
    By Maurits van der Schee, 7 years ago
  • Update README.md
    By LeaseWeb Labs, 7 years ago
  • Merge pull request #1 from ewgRa/master
    By LeaseWeb Labs, 7 years ago
  • more friendly code for kernel request listener
    By ewgRa, 7 years ago
  • small cosmetics refactoring: avoid arrays
    By ewgRa, 7 years ago
  • use Process component
    By ewgRa, 7 years ago
  • refactoring: easy relativePath function
    By ewgRa, 7 years ago
  • small refactoring: easy way for collect arguments
    By ewgRa, 7 years ago
  • strict and cosmetics
    By ewgRa, 7 years ago