2017 © Pedro Peláez
 

library lib-i18n

Internationalization library of the Ride framework

image

ride/lib-i18n

Internationalization library of the Ride framework

  • Thursday, September 8, 2016
  • by ride-user
  • Repository
  • 7 Watchers
  • 1 Stars
  • 3,141 Installations
  • PHP
  • 26 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 10 Versions
  • 2 % Grown

The README.md

Ride: I18n Library

Internationalization library of the PHP Ride framework., (*1)

What's In This Library

Locale

A Locale is the interface for a locale definition. This is a simple representation with a name, code and extra properties., (*2)

These locales are contained in a LocaleManager instance. This manager controls the current and default locale., (*3)

A LocaleIO feeds the LocaleManager with locales. The locales should be sorted in the order of importance for the system. The first locale is considered the default locale., (*4)

Negotiator

A Negotiator is used to detect the current locale. It's invoked when the current locale is not set., (*5)

You can use the ChainedNegotiator to implement multiple detection mechanisms in a chain., (*6)

Translator

A Translator is the interface to translate keys into a localized string. The interface supports a difference between singular and plural forms of a translation key., (*7)

The translators are managed by a TranslatorManager. Translator instances can be requested with the Locale instance., (*8)

I18n

The I18n class glues the different parts together into an easy facade., (*9)

Code Sample

Check this code sample to see the possibilities of this library:, (*10)

<?php

use ride\library\i18n\locale\io\LocaleIO;
use ride\library\i18n\locale\negotiator\DefaultNegotiator;
use ride\library\i18n\locale\GenericLocaleManager;
use ride\library\i18n\translator\io\AbstractTranslationIO;
use ride\library\i18n\translator\GenericTranslatorManager;
use ride\library\i18n\I18n;

/**
 * Dummy implementation of a locale data source
 */
class FooLocaleIO implements LocaleIO {

    public function getLocales() {
        return array(
            new GenericLocale('en', 'English', array(
                'full' => 'en_GB.utf8',
                'translator.script.plural': '$n != 1',
            )),
            new GenericLocale('nl', 'Nederlands', array(
                'full' => 'nl_BE.utf8',
                'translator.script.plural': '$n != 1',
            )),
        );
    }

}

/**
 * Dummy implementation of a translation data source
 */
class FooTranslationIO extends AbstractTranslationIO {

    protected function readTranslations($localeCode) {
        switch ($localeCode) {
            case 'en':
                return array(
                    'label.name' => 'Name',
                    'label.email' => 'E-mail address',
                    'label.hello' => 'Hello %name%!',
                    'label.item.0' => 'We got 1 item.',
                    'label.item.1' => 'We got %n% items.',
                );
            default:
                return array();
        } 
    }
}

// first we need to initialize our I18n instance
$localeIO = new FooLocaleIO(); 
$negotiator = new DefaultNegotiator();
$translationIO = new FooTranslationIO();

$localeManager = new GenericLocaleManager($localeIO, $negotiator);
$translatorManager = new GenericTranslatorManager($translationIO);

$i18n = new I18n($localeManager, $translatorManager);

// play with the locales
$en = $i18n->getLocale(); // default language is English since it's provided first by the locale IO
$nl = $i18n->getLocale('nl');

$i18n->setCurrentLocale($nl);
$i18n->setCurrentLocale('nl');

$nl = $i18n->getLocale();

$i18n->hasLocale('fr'); // false

// fetch some lists of the available locales
$locales = $i18n->getLocales();
$localeList = $i18n->getLocaleList(); // array('en' => 'English', 'nl' => 'Nederlands')
$localeCodeList = $i18n->getLocaleCodeList(); // array('en' => 'en', 'nl' => 'nl')

// play with translations
$translator = $i18n->getTranslator($en);

// translate some keys
$value = $translator->translate('label.name'); // Name
$value = $translator->translate('label.hello', array('name' => 'world'); // Hello world!
$value = $translator->translate('label.unexistant'); // [label.unexistant]
$value = $translator->translatePlural(1, 'label.item'); // We got 1 item. 
$value = $translator->translatePlural(3, 'label.item'); // We got 3 items. 

// translation management
$translations = $translator->getTranslations(); // array('label.name' => 'Name', 'label.email' => 'E-mail address', ...)
$translation = $translator->getTranslation('label.hello'); // Hello %name%!
$translator->setTranslation('label.foo', 'bar');

Implementations

For more examples, you can check the following implementations of this library: - ride/app-i18n - ride/web-i18n - ride/wra-i18n, (*11)

Installation

You can use Composer to install this library., (*12)

composer require ride/lib-i18n

The Versions

08/09 2016

dev-master

9999999-dev

Internationalization library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

08/09 2016

dev-develop

dev-develop

Internationalization library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

08/09 2016

1.0.1

1.0.1.0

Internationalization library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

21/06 2016

1.0.0

1.0.0.0

Internationalization library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

03/12 2015

0.1.5

0.1.5.0

Internationalization library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

11/09 2015

0.1.4

0.1.4.0

Internationalization library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

27/04 2014

0.1.3

0.1.3.0

Internationalization library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

04/04 2014

0.1.2

0.1.2.0

Internationalization library of the Ride framework

  Sources   Download

MIT

by Joris Vandeweerd

14/02 2014

0.1.1

0.1.1.0

Internationalization library of the Pallo framework

  Sources   Download

MIT

by Joris Vandeweerd

29/11 2013

0.1.0

0.1.0.0

Internationalization library of the Pallo framework

  Sources   Download

MIT

by Joris Vandeweerd