-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4889 from 3liz/backport-4888-to-release_3_9
[Backport release_3_9] New Lizmap\App\LocalesLoader to load locales for JS
- Loading branch information
Showing
9 changed files
with
752 additions
and
655 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* locale bundle loader. | ||
* | ||
* @author 3liz | ||
* @copyright 2024 3liz | ||
* | ||
* @see https://3liz.com | ||
* | ||
* @license Mozilla Public License : http://www.mozilla.org/MPL/ | ||
*/ | ||
|
||
namespace Lizmap\App; | ||
|
||
/** | ||
* Do not use directly. It will disappear when using futur Jelix versions. | ||
* | ||
* @internal | ||
*/ | ||
class LocalesBundle extends \jBundle | ||
{ | ||
/** | ||
* Get all translations of the bundle. | ||
* | ||
* @param null|string $charset | ||
* | ||
* @return array | ||
*/ | ||
public function getAllKeys($charset = null) | ||
{ | ||
if ($charset == null) { | ||
$charset = \jApp::config()->charset; | ||
} | ||
if (!in_array($charset, $this->_loadedCharset)) { | ||
$this->_loadLocales($charset); | ||
} | ||
|
||
return $this->_strings[$charset]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* locales loader. | ||
* | ||
* @author 3liz | ||
* @copyright 2024 3liz | ||
* | ||
* @see https://3liz.com | ||
* | ||
* @license Mozilla Public License : http://www.mozilla.org/MPL/ | ||
*/ | ||
|
||
namespace Lizmap\App; | ||
|
||
use jLocale; | ||
|
||
/** | ||
* Allow to load all locales from a locales file. | ||
*/ | ||
class LocalesLoader | ||
{ | ||
/** | ||
* @var LocalesBundle[][] | ||
*/ | ||
protected static $bundles = array(); | ||
|
||
/** | ||
* It returns all translations stored in the file indicated by the given key. | ||
* | ||
* @param string $key a locale key. Only module and file prefix filename is required | ||
* @param string $locale | ||
* | ||
* @throws \jExceptionSelector | ||
* | ||
* @return array all translations | ||
*/ | ||
public static function getLocalesFrom($key, $locale = null) | ||
{ | ||
// to be sure we have a valid syntax for the locale selector, we add this string. | ||
$key .= '.foo'; | ||
|
||
// With Jelix 1.9+ | ||
if (method_exists('jLocale', 'getBundle')) { | ||
return jLocale::getBundle($key, $locale)->getAllKeys(); | ||
} | ||
|
||
// with Jelix 1.8 | ||
try { | ||
$file = new \jSelectorLoc($key, $locale); | ||
} catch (\jExceptionSelector $e) { | ||
// the file is not found | ||
if ($e->getCode() == 12) { | ||
// unknown module.. | ||
throw $e; | ||
} | ||
|
||
throw new \Exception('(212)No locale file found for the given locale key "'.$key | ||
.'" in any other default languages'); | ||
} | ||
|
||
$locale = $file->locale; | ||
$keySelector = $file->module.'~'.$file->fileKey; | ||
|
||
if (!isset(self::$bundles[$keySelector][$locale])) { | ||
self::$bundles[$keySelector][$locale] = new LocalesBundle($file, $locale); | ||
} | ||
|
||
$bundle = self::$bundles[$keySelector][$locale]; | ||
|
||
return $bundle->getAllKeys(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.