Some simplification to the locale file loader

This commit is contained in:
MarkBaker 2021-05-20 20:13:06 +02:00 committed by Mark Baker
parent 8acf5e6448
commit 41a52c592c
12 changed files with 71 additions and 26 deletions

View File

@ -108,6 +108,8 @@ class LocaleGenerator
$errorCodeTranslation = "{$errorCode} = {$translationValue}" . PHP_EOL;
fwrite($configFile, $errorCodeTranslation);
} else {
$errorCodeTranslation = "{$errorCode}" . PHP_EOL;
fwrite($configFile, $errorCodeTranslation);
echo "No {$language} translation available for error code {$errorCode}", PHP_EOL;
}
}

View File

@ -2914,6 +2914,21 @@ class Calculation
return self::$localeLanguage;
}
private function getLocaleFile(string $localeDir, string $locale, string $language, string $file)
{
$localeFileName = $localeDir . str_replace('_', DIRECTORY_SEPARATOR, $locale) .
DIRECTORY_SEPARATOR . $file;
if (!file_exists($localeFileName)) {
// If there isn't a locale specific file, look for a language specific file
$localeFileName = $localeDir . $language . DIRECTORY_SEPARATOR . $file;
if (!file_exists($localeFileName)) {
throw new Exception('Locale file not found');
}
}
return $localeFileName;
}
/**
* Set the locale code.
*
@ -2921,7 +2936,7 @@ class Calculation
*
* @return bool
*/
public function setLocale($locale)
public function setLocale(string $locale)
{
// Identify our locale and language
$language = $locale = strtolower($locale);
@ -2931,23 +2946,24 @@ class Calculation
if (count(self::$validLocaleLanguages) == 1) {
self::loadLocales();
}
// Test whether we have any language data for this language (any locale)
if (in_array($language, self::$validLocaleLanguages)) {
// initialise language/locale settings
self::$localeFunctions = [];
self::$localeArgumentSeparator = ',';
self::$localeBoolean = ['TRUE' => 'TRUE', 'FALSE' => 'FALSE', 'NULL' => 'NULL'];
// Default is English, if user isn't requesting english, then read the necessary data from the locale files
if ($locale != 'en_us') {
// Default is US English, if user isn't requesting US english, then read the necessary data from the locale files
if ($locale !== 'en_us') {
$localeDir = implode(DIRECTORY_SEPARATOR, [__DIR__, 'locale', null]);
// Search for a file with a list of function names for locale
$functionNamesFile = __DIR__ . '/locale/' . str_replace('_', DIRECTORY_SEPARATOR, $locale) . DIRECTORY_SEPARATOR . 'functions';
if (!file_exists($functionNamesFile)) {
// If there isn't a locale specific function file, look for a language specific function file
$functionNamesFile = __DIR__ . '/locale/' . $language . DIRECTORY_SEPARATOR . 'functions';
if (!file_exists($functionNamesFile)) {
try {
$functionNamesFile = $this->getLocaleFile($localeDir, $locale, $language, 'functions');
} catch (Exception $e) {
return false;
}
}
// Retrieve the list of locale or language specific function names
$localeFunctions = file($functionNamesFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($localeFunctions as $localeFunction) {
@ -2967,11 +2983,12 @@ class Calculation
self::$localeBoolean['FALSE'] = self::$localeFunctions['FALSE'];
}
$configFile = __DIR__ . '/locale/' . str_replace('_', DIRECTORY_SEPARATOR, $locale) . DIRECTORY_SEPARATOR . 'config';
if (!file_exists($configFile)) {
$configFile = __DIR__ . '/locale/' . $language . DIRECTORY_SEPARATOR . 'config';
try {
$configFile = $this->getLocaleFile($localeDir, $locale, $language, 'config');
} catch (Exception $e) {
return false;
}
if (file_exists($configFile)) {
$localeSettings = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($localeSettings as $localeSetting) {
[$localeSetting] = explode('##', $localeSetting); // Strip out comments
@ -2989,7 +3006,6 @@ class Calculation
}
}
}
}
self::$functionReplaceFromExcel = self::$functionReplaceToExcel =
self::$functionReplaceFromLocale = self::$functionReplaceToLocale = null;

View File

@ -11,6 +11,7 @@ ArgumentSeparator = ;
##
## Error Codes
##
NULL
DIV0 = #DĚLENÍ_NULOU!
VALUE = #HODNOTA!
REF = #ODKAZ!

View File

@ -12,7 +12,9 @@ ArgumentSeparator = ;
## Error Codes
##
NULL = #NUL!
DIV0
VALUE = #VÆRDI!
REF = #REFERENCE!
NAME = #NAVN?
NUM
NA = #I/T

View File

@ -11,7 +11,10 @@ ArgumentSeparator = ;
##
## Error Codes
##
NULL
DIV0
VALUE = #WERT!
REF = #BEZUG!
NAME
NUM = #ZAHL!
NA = #NV

View File

@ -17,3 +17,4 @@ VALUE = #¡VALOR!
REF = #¡REF!
NAME = #¿NOMBRE?
NUM = #¡NUM!
NA

View File

@ -12,6 +12,9 @@ ArgumentSeparator = ;
## Error Codes
##
NULL = #NUL!
DIV0
VALUE = #VALEUR!
REF
NAME = #NOM?
NUM = #NOMBRE!
NA

View File

@ -11,7 +11,10 @@ ArgumentSeparator = ;
##
## Error Codes
##
NULL
DIV0
VALUE = #VALORE!
REF = #RIF!
NAME = #NOME?
NUM
NA = #N/D

View File

@ -11,6 +11,10 @@ ArgumentSeparator = ;
##
## Error Codes
##
NULL
DIV0
VALUE = #VERDI!
REF
NAME = #NAVN?
NUM
NA = #N/D

View File

@ -12,7 +12,9 @@ ArgumentSeparator = ;
## Error Codes
##
NULL = #NULO!
DIV0
VALUE = #VALOR!
REF
NAME = #NOME?
NUM = #NÚM!
NA = #N/D

View File

@ -12,7 +12,9 @@ ArgumentSeparator = ;
## Error Codes
##
NULL = #NULO!
DIV0
VALUE = #VALOR!
REF
NAME = #NOME?
NUM = #NÚM!
NA = #N/D

View File

@ -95,6 +95,12 @@ class CalculationTest extends TestCase
self::assertTrue($calculation->setLocale($locale));
}
public function testInvalidLocaleReturnsFalse(): void
{
$calculation = Calculation::getInstance();
self::assertFalse($calculation->setLocale('xx'));
}
public function providerCanLoadAllSupportedLocales(): array
{
return [