2017 © Pedro Peláez
 

library internationalization

A PHP package to manage i18n: translations, pluralization and date formats according to a localization.

image

atelierspierrot/internationalization

A PHP package to manage i18n: translations, pluralization and date formats according to a localization.

  • Saturday, January 2, 2016
  • by picas
  • Repository
  • 1 Watchers
  • 2 Stars
  • 332 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Internationalization

demonstration documentation Code Climate, (*1)

A PHP package to manage i18n: translations, pluralization, date and number formatting according to a localization., (*2)

Usage

Object creation and PHP usage

To create a new I18n instance, you need to pass it a I18n\Loader object:, (*3)

// Creation of the I18n Loader
$i18n_loader = new \I18n\Loader(array(

    // this is the directory where your language strings are defined
    'language_directory' => __DIR__.'/i18n',

    // this is the list of available languages
    'available_languages' => array(
        'en' => 'en_US_USD',
        'gb' => 'en_GB_UKP',
        'fr' => 'fr_FR_EUR'
    ),
    'default_language' => 'en',

    // this is the tag construction used for replacements in strings
    // by default, "%arg%" will be replacement by the argument "arg" value
    // as this will be passed to a 'sprintf' PHP function, literal percent is written '%%'
    'arg_wrapper_mask' => "%%%s%%",
));

// Creation of the I18n instance (statically) passing it the Loader
$translator = \I18n\I18n::getInstance($i18n_loader);

For a full list of possible Loader options, please have a look in source code., (*4)

Any option value defining a directory path or a filename construction can contains a %s tag that will be replaced by the current language., (*5)

// for instance:
'language_directory' => __DIR__.'/i18n/%s'

// will render, for the EN language:
'language_directory' => __DIR__.'/i18n/EN'

As you can see, the I18n class is defined as a Singleton object: any future call of \I18n\I18n::getInstance() will refer to the first created object instance., (*6)

Then, to actually use the translated value of a string, use the translate method:, (*7)

$translator->translate( 'string_index' [, array( arguments )] [, language code] )

You can use the pluralize method to choose a translated string depending on a number of items:, (*8)

$indexes = array(
    0=>'test_item_zero',
    1=>'test_item_one',
    2=>'test_item_two',
    3=>'test_item_multi'
);
$translator->pluralize( $indexes, $number_of_items [, array( arguments )] [, language code] )

Translation strings definition

By default (this can be over-write in the Loader), the I18n object will load the strings defined as a PHP array like:, (*9)

$i18n_en = array (
  'datetime_mask' => '%a %e %m %Y %H:%M:%S',
  'test' => 'Test in english',
  'test_args' => 'I received arguments : « %arg1% » and « %arg2% »',
  'test_item_zero' => 'No item',
  'test_item_one' => 'Just one item',
  'test_item_two' => 'Two items',
  'test_item_multi' => 'There are %nb% items',
);

This may be defined in a file called i18n.CODE.php where CODE is the two letter reference of the language. These files will be searched and loaded from the language_directory loader option value., (*10)

Load multiple language files

The I18n object is designed to be able to load multiple language files easily with:, (*11)

$i18n->loadFile( my file path )

Each file loaded is stored in the internal cache system of the object (a simple PHP array)., (*12)

Package aliases

A set of aliases functions, defined in the global namespace, are available and auto-loaded by Composer:, (*13)

function _T(...) = $i18n->translate(...)
#    or
function translate(...) = $i18n->translate(...)

function _P(...) = $i18n->pluralize(...)
#    or
function pluralize(...) = $i18n->pluralize(...)

function _D(...) = $i18n->getLocalizedDateString(...)
#    or
function datify(...) = $i18n->getLocalizedDateString(...)

Extensions

The package embeds an extension to use the class in the Twig template engine. See the Twig Extension page for more infos., (*14)

Installation

For a complete information about how to install this package and load its namespace, please have a look at our USAGE documentation., (*15)

If you are a Composer user, just add the package to the requirements of your project's composer.json manifest file:, (*16)

"atelierspierrot/internationalization": "@stable"

You can use a specific release or the latest release of a major version using the appropriate version constraint., (*17)

Please note that this package depends on the externals PHP Patterns and PHP Library., (*18)

Author & License

Internationalization, (*19)

http://github.com/atelierspierrot/internationalization, (*20)

Copyright (c) 2010-2016, Pierre Cassat and contributors, (*21)

Licensed under the Apache 2.0 license., (*22)

http://www.apache.org/licenses/LICENSE-2.0, (*23)


Les Ateliers Pierrot - Paris, France, (*24)

http://www.ateliers-pierrot.fr/ - contact@ateliers-pierrot.fr, (*25)

The Versions

02/01 2016

dev-master

9999999-dev http://github.com/atelierspierrot/internationalization

A PHP package to manage i18n: translations, pluralization and date formats according to a localization.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

i18n internationalization localization

02/01 2016

dev-dev

dev-dev http://github.com/atelierspierrot/internationalization

A PHP package to manage i18n: translations, pluralization and date formats according to a localization.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

i18n internationalization localization

04/03 2015

v1.1.0

1.1.0.0 http://github.com/atelierspierrot/internationalization

A PHP package to manage i18n: translations, pluralization and date formats according to a localization.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

i18n internationalization localization

04/01 2015

v1.0.3

1.0.3.0 http://github.com/atelierspierrot/internationalization

A PHP package to manage i18n: translations, pluralization and date formats according to a localization.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

i18n internationalization localization

22/06 2014

v1.0.2

1.0.2.0 http://github.com/atelierspierrot/internationalization

A PHP package to manage i18n: translations, pluralizations and date formats according to a localization.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

i18n internationalization localization

14/06 2014

v1.0.1

1.0.1.0 http://github.com/atelierspierrot/internationalization

A PHP package to manage i18n: translations, pluralizations and date formats according to a localization.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

i18n internationalization localization

15/10 2013

v1.0.0

1.0.0.0 http://github.com/atelierspierrot/internationalization

A PHP package to manage i18n: translations, pluralizations and date formats according to a localization.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

i18n internationalization localization