2017 © Pedro Peláez
 

library i18n

Easy library to manage i18n with PHP.

image

o80/i18n

Easy library to manage i18n with PHP.

  • Thursday, May 12, 2016
  • by olivierperez
  • Repository
  • 4 Watchers
  • 10 Stars
  • 10,018 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 3 Open issues
  • 6 Versions
  • 11 % Grown

The README.md

o80-i18n

o80-i18n is a small PHP tool to manage i18n (internationalization). By default it uses json format for language files, you can define your own provider if you prefer another format. See below how the usage is simple., (*1)

Build Status Latest Unstable Version License, (*2)

How-to

Installation

With Composer, you simply need to require o80/i18n:, (*3)

{
...
    "require": {
        "o80/i18n": "~0.2"
    }
...
}

Usage

Dictionaries

For instance, put your language files in 'lang' directory :, (*4)

  • langs
    • en.json
    • en_US.json
    • fr.json

Example of language file en.json :, (*5)

{
    "Generic" : {
        "Welcome": "Welcome",
        "Hello": "Hello %s!",
        "submit": "submit"
    }
}

Example of language file fr.json :, (*6)

{
    "Generic" : {
        "Welcome": "Bienvenue",
        "Hello": "Bonjour %s !",
        "submit": "valider"
    }
}

Configure the i18n instance

Way 1 - Singleton

$i18n = I18N::instance();
$i18n->setPath(__DIR__ . '/langs');
$i18n->setDefaultLang('en');

Way 2 - New instance

$i18n = new I18N();
$i18n->setPath(__DIR__ . '/langs');
$i18n->setDefaultLang('en');

Usage

Way 1 - Singleton

<h1><?php echo __('Generic', 'Welcome'); ?></h1>
<!-- Result : <h1>Welcome</h1> -->
<h1><?php echo __('Generic', 'NotExistingText'); ?></h1>
<!-- Result : <h1>[missing key: Generic.NotExistingText]</h1> -->
<span><?php echo __f('Generic', 'Hello', 'Olivier'); ?></span>
<!-- Result : <span>Hello Olivier!</span> -->
<span><?php echo I18N::instance()->getLoadedLang(); ?></span>
<!-- Result : <span>en</span> -->

Way 2 - With the instance

<h1><?php echo $i18n->get('Generic', 'Welcome'); ?></h1>
<!-- Result : <h1>Welcome</h1> -->
<h1><?php echo $i18n->get('Generic', 'NotExistingText'); ?></h1>
<!-- Result : <h1>[missing key: Generic.NotExistingText]</h1> -->
<span><?php echo $i18n->format('Generic', 'Hello', array('Olivier')); ?></span>
<!-- Result : <span>Hello Olivier!</span> -->
<span><?php echo $i18n->getLoadedLang(); ?></span>
<!-- Result : <span>en</span> -->

How to set the language to use

The system look into serverals variables to find the language file to load. Look below to understand it., (*7)

  • Use $_GET['lang'] if it's defined and if it matches to a language file;
  • Use $_SESSION['lang'] if it's defined and if it matches to a language file;
  • Use $_SERVER['HTTP_ACCEPT_LANGUAGE'] if it's defined and if it matches to a language file;
    • It checks for all languages found in this variable
  • Use the $defaultLang you defined in the I18N instance
  • If no file found, don't do load anything

Contribution

Just fork the project, make your changes, ask for pull request ;-)., (*8)

The Versions

12/05 2016

dev-master

9999999-dev https://github.com/olivierperez/o80-i18n

Easy library to manage i18n with PHP.

  Sources   Download

Apache License 2.0

The Requires

  • php >=5.3.0

 

The Development Requires

php i18n internationalization

12/05 2016

0.4

0.4.0.0 https://github.com/olivierperez/o80-i18n

Easy library to manage i18n with PHP.

  Sources   Download

Apache License 2.0

The Requires

  • php >=5.3.0

 

The Development Requires

php i18n internationalization

21/09 2015

dev-develop

dev-develop https://github.com/olivierperez/o80-i18n

Easy library to manage i18n with PHP.

  Sources   Download

Apache License 2.0

The Requires

  • php >=5.3.0

 

The Development Requires

php i18n internationalization

31/03 2015

0.3

0.3.0.0 https://github.com/olivierperez/o80-i18n

Easy library to manage i18n with PHP.

  Sources   Download

Apache License 2.0

The Requires

  • php >=5.3.0

 

The Development Requires

php i18n internationalization

27/03 2015

0.2

0.2.0.0 https://github.com/olivierperez/o80-i18n

Easy library to manage i18n with PHP.

  Sources   Download

Apache License 2.0

The Requires

  • php >=5.3.0

 

The Development Requires

php i18n internationalization

19/03 2015

0.1

0.1.0.0 https://github.com/olivierperez/o80-i18n

Easy library to manage i18n with PHP.

  Sources   Download

Apache License 2.0

The Requires

  • php >=5.3.0

 

The Development Requires

php i18n internationalization