2017 © Pedro Peláez
 

library simplelocalization

A simple class to manipulate localized messages

image

jelix/simplelocalization

A simple class to manipulate localized messages

  • Monday, December 11, 2017
  • by laurentj
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,093 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 8 % Grown

The README.md

This is a simple class to load and give translated strings, for localization., (*1)

It was formerly used in the Jelix Framework., (*2)

installation

You can install it from Composer. In your project:, (*3)

composer require "jelix/simplelocalization"

Usage

First create lang files. You can store strings of all lang into a single file, or in one file per lang. Each file is a PHP array, and each strings have a key., (*4)

Example. Here is a file translation.en.php for english strings:, (*5)

<?php

return array(
    'welcome' => 'Hello world',
    'thank.you' => 'Thank you'
);

Now translation.fr.php for french strings:, (*6)

<?php

return array(
    'welcome' => 'Bonjour le monde',
    'thank.you' => 'Merci'
);

Or if you choose to have a single file translation.php :, (*7)

<?php

return array(
    'en' => array(
        'welcome' => 'Hello world',
        'thank.you' => 'Thank you'
    ),
    'fr' => array(
        'welcome' => 'Bonjour le monde',
        'thank.you' => 'Merci'
    )
);

Now in your PHP code, instanciate a container:, (*8)


// with multiple files $locales = new \Jelix\SimpleLocalization\Container('translation.%LANG%.php'); // note that %LANG% in the path will be replace by a lang code // with a single file $locales = new \Jelix\SimpleLocalization\Container('translation.php'); // with an array of file name. You can mix path with `%LANG%` tag and path without it $locales = new \Jelix\SimpleLocalization\Container(array('translation.%LANG%.php', 'other_translation.php')); // with an array of strings $locales = new \Jelix\SimpleLocalization\Container(array( 'en' => array( 'welcome' => 'Bonjour le monde', 'thank.you' => 'Merci' ), 'fr' => array( 'welcome' => 'Bonjour le monde', 'thank.you' => 'Merci' ) ));

As second parameter to the constructor, you can give a lang code. If not given the current lang will be guessed from $_SERVER['HTTP_ACCEPT_LANGUAGE'];, (*9)

Now, to retrieve a string from its key:, (*10)


$str = $locales->get('welcome');

That's it..., (*11)

The Versions

11/12 2017

dev-master

9999999-dev http://jelix.org

A simple class to manipulate localized messages

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.3

 

The Development Requires

translation locale l10n

27/11 2015

v1.7.1

1.7.1.0 http://jelix.org

A simple class to manipulate localized messages

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.3

 

The Development Requires

27/11 2015

v1.7.0

1.7.0.0 http://jelix.org

A simple class to manipulate localized messages

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.3

 

The Development Requires