2017 © Pedro Peláez
 

library i18n

An i18n library for PHP

image

philasearch/i18n

An i18n library for PHP

  • Wednesday, March 8, 2017
  • by tmuntan1
  • Repository
  • 1 Watchers
  • 1 Stars
  • 6,283 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 5 Versions
  • 4 % Grown

The README.md

Latest Stable Version Total Downloads Build Status, (*1)

PHP I18n

PHP I18n is a simple library for storing localized strings outside of your code., (*2)

Installing

Add the following require to your composer.json file., (*3)

{
    "require": {
        "philasearch/i18n": "1.1.*"
    }
}

Setup

Language Folder

In order to use i18n, you will need a language folder. By convention this folder is config/locales, however, you can choose whichever you want. In this folder, each supported language requires its own folder (config/locales/en, config/locales/de, ...)., (*4)

Use in PHP

You will most likely be using the I18n class in your own implementation, so you are just given a class which you can write a wrapper class for in your own code. All you need to do to create this class is give it the path to the language folder you decided on before., (*5)

<?php

use Philasearch\I18n\I18n as Lang;

$lang = new Lang( dirname(__FILE__) . '/config/locales' );

Example Usage

Notice: Currently, only yaml files are supported., (*6)

For this example, the language folder is at config/locales., (*7)

Create a file config/locales/en/example.yml, (*8)

foo: bar
nested:
  foo: bar
var: foo :var
plural:
  one: A :color apple
  other: :count :color apples

And a dialect file config/locales/en_US/example.yml, (*9)

foo: murica

Note: Take note of the yml file name as it will be the first part of your key (example.yml => example)., (*10)

Now we can load the translation class and get our translated string., (*11)

<?php

use Philasearch\I18n\I18n as Lang;

$lang = new Lang( dirname(__FILE__) . 'config/locales' );

/**
 * normal translated string
 *
 * @param string $locale
 * @param string $key
 */
$lang->get( 'en', 'example.foo' ); // returns 'bar'


/**
 * nested translated strings
 *
 * @param string $locale
 * @param string $key
 */
$lang->get( 'en', 'example.nested.foo' ); // returns 'bar'

/**
 * Variable translated string
 *
 * @param string $locale
 * @param string $key
 */
$lang->get( 'en', 'example.var', ['var' => 'bar']); // returns 'foo bar'

/**
 * Plural tranlated strings
 *
 * @param string $locale
 * @param string $key
 * @param int    $count
 */
$lang->get( 'en', 'example.plural', ['color' => 'red'], 1); // returns 'A red apple'
$lang->get( 'en', 'example.plural', ['color' => 'red'], 2); // returns '2 red apples'

/**
 * Dialect translated strings
 */
$lang->get( 'en_US', 'example.foo' );         // returns 'murica'
$lang->get( 'en_US', 'example.nested.foo' );  // returns 'bar' as it falls back to en

The Versions

08/03 2017

dev-master

9999999-dev https://github.com/philasearch/php-i18n

An i18n library for PHP

  Sources   Download

The Requires

 

The Development Requires

by Thomas Muntaner

08/03 2017

1.1.1

1.1.1.0 https://github.com/philasearch/php-i18n

An i18n library for PHP

  Sources   Download

The Requires

 

The Development Requires

by Thomas Muntaner

08/03 2017

1.1.0

1.1.0.0 https://github.com/philasearch/php-i18n

An i18n library for PHP

  Sources   Download

The Requires

 

The Development Requires

by Thomas Muntaner

15/11 2016

1.0.5

1.0.5.0 https://github.com/philasearch/php-i18n

An i18n library for PHP

  Sources   Download

The Requires

 

The Development Requires

by Thomas Muntaner

09/09 2014

1.0.0

1.0.0.0 https://github.com/philasearch/php-i18n

An i18n library for PHP

  Sources   Download

The Requires

 

The Development Requires

by Thomas Muntaner