Overview

Build Status Codacy Badge

Vespula Locale is a simple class for displaying localized strings. It is loosely based on Paul M. Jones' SolarPHP locale implementation.

Basic Usage

  1. place your locale files into a folder of your choice using xx_YY.php for the naming convention. For example, en_CA.php or en_US.php
  2. all your locale files should be in the same folder.
  3. when defining your strings, you can use an array. The first element is the singular version. The second is the plural version.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php
/* en_CA.php */
return [
    'TEXT_HOME'=>'Home',
    'TEXT_APPLE'=>['apple', 'apples'] // singlular and plural version of the word
];

/* fr_CA.php */
return [
    'TEXT_HOME'=>'Accueil',
    'TEXT_APPLE'=>['pomme', 'pommes']
];

Now, instantiate the Vespula\Locale\Locale object and load the locales. Then, you are ready to display localized strings based on a key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
$locale = new \Vespula\Locale\Locale('en_CA');
$locale->load('/path/to/locales');

// show all the strings for each language code
print_r($locale->getStrings());

// Get a single string
echo $locale->gettext('TEXT_HOME');

// Get a pluralized form of the string
// The second parameter is the number of objects you have.
echo $locale->gettext('TEXT_APPLE', 4);
// displays 'apples'

// switch langs
$locale->setCode('fr_CA');
echo $locale->gettext('TEXT_APPLE', 4);
// displays 'pommes'