Skip to content
Snippets Groups Projects
Commit d1ccfba5 authored by Demian Katz's avatar Demian Katz
Browse files

Added fallback mechanism.

parent 5e61192d
No related merge requests found
...@@ -48,14 +48,33 @@ class ExtendedIni implements FileLoaderInterface ...@@ -48,14 +48,33 @@ class ExtendedIni implements FileLoaderInterface
*/ */
protected $pathStack; protected $pathStack;
/**
* Fallback locale to use for language strings missing from selected file.
*
* @var string
*/
protected $fallbackLocale;
/**
* List of files loaded during the current run -- avoids infinite loops and
* duplicate loading.
*
* @var array
*/
protected $loadedFiles = array();
/** /**
* Constructor * Constructor
* *
* @param array $pathStack List of directories to search for language files. * @param array $pathStack List of directories to search for language
* files.
* @param string $fallbackLocale Fallback locale to use for language strings
* missing from selected file.
*/ */
public function __construct($pathStack = array()) public function __construct($pathStack = array(), $fallbackLocale = null)
{ {
$this->pathStack = $pathStack; $this->pathStack = $pathStack;
$this->fallbackLocale = $fallbackLocale;
} }
/** /**
...@@ -70,8 +89,46 @@ class ExtendedIni implements FileLoaderInterface ...@@ -70,8 +89,46 @@ class ExtendedIni implements FileLoaderInterface
*/ */
public function load($locale, $filename) public function load($locale, $filename)
{ {
// Reset the loaded files list:
$this->resetLoadedFiles();
// Load base data: // Load base data:
return $this->loadLanguageFile($locale . '.ini'); $data = $this->loadLanguageFile($locale . '.ini');
// Load fallback data, if any:
if (!empty($this->fallbackLocale)) {
$newData = $this->loadLanguageFile($this->fallbackLocale . '.ini');
$newData->merge($data);
$data = $newData;
}
return $data;
}
/**
* Reset the loaded file list.
*
* @return void
*/
protected function resetLoadedFiles()
{
$this->loadedFiles = array();
}
/**
* Check if a file has already been loaded; mark it loaded if it is not already.
*
* @param string $filename Name of file to check and mark as loaded.
*
* @return bool True if loaded, false if new.
*/
protected function checkAndMarkLoadedFile($filename)
{
if (isset($this->loadedFiles[$filename])) {
return true;
}
$this->loadedFiles[$filename] = true;
return false;
} }
/** /**
...@@ -83,6 +140,11 @@ class ExtendedIni implements FileLoaderInterface ...@@ -83,6 +140,11 @@ class ExtendedIni implements FileLoaderInterface
*/ */
protected function loadLanguageFile($filename) protected function loadLanguageFile($filename)
{ {
// Don't load a file that has already been loaded:
if ($this->checkAndMarkLoadedFile($filename)) {
return new TextDomain();
}
$data = false; $data = false;
foreach ($this->pathStack as $path) { foreach ($this->pathStack as $path) {
if (file_exists($path . '/' . $filename)) { if (file_exists($path . '/' . $filename)) {
......
test3 = "test three"
\ No newline at end of file
...@@ -64,6 +64,50 @@ class ExtendedIniTest extends \VuFindTest\Unit\TestCase ...@@ -64,6 +64,50 @@ class ExtendedIniTest extends \VuFindTest\Unit\TestCase
); );
} }
/**
* Test fallback to a different language.
*
* @return void
*/
public function testFallback()
{
$pathStack = array(
realpath(__DIR__ . '/../../../../../../fixtures/language/base'),
);
$loader = new ExtendedIni($pathStack, 'en');
$result = $loader->load('fake', null);
$this->assertEquals(
array(
'blank_line' =>
html_entity_decode('‌', ENT_NOQUOTES, 'UTF-8'),
'test1' => 'test one',
'test2' => 'test two',
'test3' => 'test three',
),
(array)$result
);
}
/**
* Test fallback to the same language.
*
* @return void
*/
public function testFallbackToSelf()
{
$pathStack = array(
realpath(__DIR__ . '/../../../../../../fixtures/language/base'),
);
$loader = new ExtendedIni($pathStack, 'fake');
$result = $loader->load('fake', null);
$this->assertEquals(
array(
'test3' => 'test three',
),
(array)$result
);
}
/** /**
* Test missing path stack. * Test missing path stack.
* *
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment