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; $errorCodeTranslation = "{$errorCode} = {$translationValue}" . PHP_EOL;
fwrite($configFile, $errorCodeTranslation); fwrite($configFile, $errorCodeTranslation);
} else { } else {
$errorCodeTranslation = "{$errorCode}" . PHP_EOL;
fwrite($configFile, $errorCodeTranslation);
echo "No {$language} translation available for error code {$errorCode}", PHP_EOL; echo "No {$language} translation available for error code {$errorCode}", PHP_EOL;
} }
} }

View File

@ -2914,6 +2914,21 @@ class Calculation
return self::$localeLanguage; 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. * Set the locale code.
* *
@ -2921,7 +2936,7 @@ class Calculation
* *
* @return bool * @return bool
*/ */
public function setLocale($locale) public function setLocale(string $locale)
{ {
// Identify our locale and language // Identify our locale and language
$language = $locale = strtolower($locale); $language = $locale = strtolower($locale);
@ -2931,23 +2946,24 @@ class Calculation
if (count(self::$validLocaleLanguages) == 1) { if (count(self::$validLocaleLanguages) == 1) {
self::loadLocales(); self::loadLocales();
} }
// Test whether we have any language data for this language (any locale) // Test whether we have any language data for this language (any locale)
if (in_array($language, self::$validLocaleLanguages)) { if (in_array($language, self::$validLocaleLanguages)) {
// initialise language/locale settings // initialise language/locale settings
self::$localeFunctions = []; self::$localeFunctions = [];
self::$localeArgumentSeparator = ','; self::$localeArgumentSeparator = ',';
self::$localeBoolean = ['TRUE' => 'TRUE', 'FALSE' => 'FALSE', 'NULL' => 'NULL']; 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 // Search for a file with a list of function names for locale
$functionNamesFile = __DIR__ . '/locale/' . str_replace('_', DIRECTORY_SEPARATOR, $locale) . DIRECTORY_SEPARATOR . 'functions'; try {
if (!file_exists($functionNamesFile)) { $functionNamesFile = $this->getLocaleFile($localeDir, $locale, $language, 'functions');
// If there isn't a locale specific function file, look for a language specific function file } catch (Exception $e) {
$functionNamesFile = __DIR__ . '/locale/' . $language . DIRECTORY_SEPARATOR . 'functions'; return false;
if (!file_exists($functionNamesFile)) {
return false;
}
} }
// Retrieve the list of locale or language specific function names // Retrieve the list of locale or language specific function names
$localeFunctions = file($functionNamesFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $localeFunctions = file($functionNamesFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($localeFunctions as $localeFunction) { foreach ($localeFunctions as $localeFunction) {
@ -2967,24 +2983,24 @@ class Calculation
self::$localeBoolean['FALSE'] = self::$localeFunctions['FALSE']; self::$localeBoolean['FALSE'] = self::$localeFunctions['FALSE'];
} }
$configFile = __DIR__ . '/locale/' . str_replace('_', DIRECTORY_SEPARATOR, $locale) . DIRECTORY_SEPARATOR . 'config'; try {
if (!file_exists($configFile)) { $configFile = $this->getLocaleFile($localeDir, $locale, $language, 'config');
$configFile = __DIR__ . '/locale/' . $language . DIRECTORY_SEPARATOR . '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
if (strpos($localeSetting, '=') !== false) {
[$settingName, $settingValue] = array_map('trim', explode('=', $localeSetting));
$settingName = strtoupper($settingName);
if ($settingValue !== '') {
switch ($settingName) {
case 'ARGUMENTSEPARATOR':
self::$localeArgumentSeparator = $settingValue;
break; $localeSettings = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
} foreach ($localeSettings as $localeSetting) {
[$localeSetting] = explode('##', $localeSetting); // Strip out comments
if (strpos($localeSetting, '=') !== false) {
[$settingName, $settingValue] = array_map('trim', explode('=', $localeSetting));
$settingName = strtoupper($settingName);
if ($settingValue !== '') {
switch ($settingName) {
case 'ARGUMENTSEPARATOR':
self::$localeArgumentSeparator = $settingValue;
break;
} }
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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