Add a test for the localize function. re #96

This commit is contained in:
Sébastien Lucas 2013-10-04 12:20:34 +02:00
parent 3be4fce3a5
commit 9c09b8797c
2 changed files with 27 additions and 1 deletions

View file

@ -139,7 +139,7 @@ function str_format($format) {
* This method is based on this page * This method is based on this page
* http://www.mind-it.info/2010/02/22/a-simple-approach-to-localization-in-php/ * http://www.mind-it.info/2010/02/22/a-simple-approach-to-localization-in-php/
*/ */
function localize($phrase, $count=-1) { function localize($phrase, $count=-1, $reset=false) {
if ($count == 0) if ($count == 0)
$phrase .= ".none"; $phrase .= ".none";
if ($count == 1) if ($count == 1)
@ -149,6 +149,9 @@ function localize($phrase, $count=-1) {
/* Static keyword is used to ensure the file is loaded only once */ /* Static keyword is used to ensure the file is loaded only once */
static $translations = NULL; static $translations = NULL;
if ($reset) {
$translations = NULL;
}
/* If no instance of $translations has occured load the language file */ /* If no instance of $translations has occured load the language file */
if (is_null($translations)) { if (is_null($translations)) {
$lang = "en"; $lang = "en";

View file

@ -11,4 +11,27 @@ class BaseTest extends PHPUnit_Framework_TestCase
$this->assertEquals ("?key=value&db=0", addURLParameter ("?key=value", "db", "0")); $this->assertEquals ("?key=value&db=0", addURLParameter ("?key=value", "db", "0"));
$this->assertEquals ("?key=value&otherKey=&db=0", addURLParameter ("?key=value&otherKey", "db", "0")); $this->assertEquals ("?key=value&otherKey=&db=0", addURLParameter ("?key=value&otherKey", "db", "0"));
} }
public function testLocalize ()
{
$this->assertEquals ("Authors", localize ("authors.title"));
}
public function testLocalizeFr ()
{
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3";
$this->assertEquals ("Auteurs", localize ("authors.title", -1, true));
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
localize ("authors.title", -1, true);
}
public function testLocalizeUnknown ()
{
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "aa";
$this->assertEquals ("Authors", localize ("authors.title", -1, true));
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
localize ("authors.title", -1, true);
}
} }