From 34d7751cbf5712c8b9665f516748b7e7222cd454 Mon Sep 17 00:00:00 2001 From: Ron Wardenier Date: Fri, 10 Jun 2022 11:59:44 +0200 Subject: [PATCH 1/4] Make case-insensitive keys and allow use of '.' as section separator Make case-insensitive keys and allow use of a '.' as section separator. Using a '.' as section separator works in conjunction with the helper function only. --- i18n.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/i18n.class.php b/i18n.class.php index c4a4ec2..2f531f8 100644 --- a/i18n.class.php +++ b/i18n.class.php @@ -351,9 +351,9 @@ protected function compile($config, $prefix = '') { $code = ''; foreach ($config as $key => $value) { if (is_array($value)) { - $code .= $this->compile($value, $prefix . $key . $this->sectionSeparator); + $code .= $this->compile($value, $prefix . strtolower($key) . str_replace('.', '_', $this->sectionSeparator)); } else { - $fullName = $prefix . $key; + $fullName = $prefix . strtolower($key); if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $fullName)) { throw new InvalidArgumentException(__CLASS__ . ": Cannot compile translation key " . $fullName . " because it is not a valid PHP identifier."); } From 71be7fbee79577cb3a75184fd5f6859f07ae9b47 Mon Sep 17 00:00:00 2001 From: Ron Wardenier Date: Fri, 10 Jun 2022 12:33:41 +0200 Subject: [PATCH 2/4] Updated ReadMe --- README.md | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5beec8d..f3df64f 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,19 @@ This is a simple i18n class for PHP. Nothing fancy, but fast, because it uses ca Some of its features: -* Translation strings in `.ini`/`.properties`, `.json` or `.yaml` format -* Caching -* Simple API: `L::category_stringname` -* Built-in support for [vsprintf](http://php.net/manual/en/function.vsprintf.php) formatting: `L::name($par1)` -* Automatic user language detection -* Simplicity ;) +- Translation strings in `.ini`/`.properties`, `.json` or `.yaml` format +- Case-insensitive keys +- Caching +- Simple API: `L::category_stringname` +- Built-in support for [vsprintf](http://php.net/manual/en/function.vsprintf.php) formatting: `L::name($par1)` +- Automatic user language detection +- Simplicity ;) ## Requirements -* Write permissions in cache directory -* PHP 5.2 and above -* PHP SPL extension (installed by default) +- Write permissions in cache directory +- PHP 5.2 and above +- PHP SPL extension (installed by default) ## Setup @@ -57,6 +58,7 @@ The files must be named according to the filePath setting, where '{LANGUAGE}' wi ``` ### 3. Initialize the class + ```php Date: Fri, 10 Jun 2022 12:44:03 +0200 Subject: [PATCH 3/4] Updated ReadMe --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f3df64f..673622e 100644 --- a/README.md +++ b/README.md @@ -148,8 +148,8 @@ As you can see, you can also call the constant as a function. It will be formatt Also, like in the two last examples, a helper function with the same name as the class makes it easier to dynamically access the constants if ever needed. -When using the helper function a dot `.` may be as the section separator, for example `echo L("category.somethingother");`. -Note this will not work when using the constant notation as in PHP the dot is not a valid character in constant names. +When using the helper function a dot `.` may be used as the section separator, for example `echo L("category.somethingother");`. +Note: this will not work when using the constant notation as in PHP the dot is not a valid character in constant names. That's it! From 3be290687fc54223f6c6c0e07ecbaea07a077922 Mon Sep 17 00:00:00 2001 From: Ron Wardenier Date: Fri, 10 Jun 2022 14:02:33 +0200 Subject: [PATCH 4/4] Make case-insensitive keys and allow use of '.' as section separator Make case-insensitive keys and allow use of a '.' as section separator. Using a '.' as section separator works in conjunction with the helper function only. --- i18n.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/i18n.class.php b/i18n.class.php index 2f531f8..c6f1487 100644 --- a/i18n.class.php +++ b/i18n.class.php @@ -169,6 +169,8 @@ public function init() { . ' return vsprintf(constant("self::" . $string), $args);' . "\n}\n}\n" . "function ".$this->prefix .'($string, $args=NULL) {'."\n" + . ' $string = str_replace(\'.\', \'_\', $string);'."\n" + . ' $string = strtolower($string);'."\n" . ' $return = constant("'.$this->prefix.'::".$string);'."\n" . ' return $args ? vsprintf($return,$args) : $return;' . "\n}"; @@ -351,9 +353,9 @@ protected function compile($config, $prefix = '') { $code = ''; foreach ($config as $key => $value) { if (is_array($value)) { - $code .= $this->compile($value, $prefix . strtolower($key) . str_replace('.', '_', $this->sectionSeparator)); + $code .= $this->compile($value, $prefix . $key . str_replace('.', '_', $this->sectionSeparator)); } else { - $fullName = $prefix . strtolower($key); + $fullName = $prefix . $key; if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $fullName)) { throw new InvalidArgumentException(__CLASS__ . ": Cannot compile translation key " . $fullName . " because it is not a valid PHP identifier."); }