Skip to content

Commit

Permalink
Merge pull request #4889 from 3liz/backport-4888-to-release_3_9
Browse files Browse the repository at this point in the history
[Backport release_3_9] New Lizmap\App\LocalesLoader to load locales for JS
  • Loading branch information
laurentj authored Oct 18, 2024
2 parents 0986c00 + ebfb826 commit 7ae4a94
Show file tree
Hide file tree
Showing 9 changed files with 752 additions and 655 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/php-stan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ jobs:
uses: shivammathur/setup-php@v2
# About the PHP version, do not forget to change in PHP Unit as well
with:
php-version: '8.0'
php-version: '8.2'
tools: phpstan:1.11.11
ini-values: error_reporting=22527

- name: Cache dependencies
uses: actions/cache@v4
Expand All @@ -50,7 +51,7 @@ jobs:
- name: Run PHPStan
id: test-phpstan
run: phpstan analyse -c ../phpstan.neon
run: phpstan analyse -c ../phpstan-ci.neon

- name: Notify in case of failure
uses: peter-evans/commit-comment@v3
Expand Down
4 changes: 2 additions & 2 deletions lizmap/modules/lizmap/classes/lizmap.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static function getRepositoryList()
public static function getRepositoryProperties()
{
trigger_error('This method is deprecated. Please use the lizmapRepository::getProperties() method.', E_DEPRECATED);

// @phpstan-ignore deadCode.unreachable
return lizmapRepository::$properties;
}

Expand All @@ -150,7 +150,7 @@ public static function getRepositoryProperties()
public static function getRepositoryPropertiesOptions()
{
trigger_error('This method is deprecated. Please use the lizmapRepository::getPropertiesOptions() method.', E_DEPRECATED);

// @phpstan-ignore deadCode.unreachable
return lizmapRepository::$propertiesOptions;
}

Expand Down
40 changes: 40 additions & 0 deletions lizmap/modules/lizmap/lib/App/LocalesBundle.php
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];
}
}
72 changes: 72 additions & 0 deletions lizmap/modules/lizmap/lib/App/LocalesLoader.php
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();
}
}
36 changes: 7 additions & 29 deletions lizmap/modules/view/controllers/translate.classic.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use Lizmap\App\LocalesLoader;

/**
* Service to provide translation dictionnary.
*
Expand Down Expand Up @@ -29,22 +32,10 @@ public function index()
$lang = $this->param('lang');

if (!$lang) {
$lang = jLocale::getCurrentLang().'_'.jLocale::getCurrentCountry();
$lang = jLocale::getCurrentLocale();
}

$data = array();
$path = jApp::appPath().'modules/view/locales/en_US/dictionnary.UTF-8.properties';
if (file_exists($path)) {
$lines = file($path);
foreach ($lines as $lineNumber => $lineContent) {
if (!empty($lineContent) and $lineContent != '\n') {
$exp = explode('=', trim($lineContent));
if (!empty($exp[0])) {
$data[$exp[0]] = jLocale::get('view~dictionnary.'.$exp[0], null, $lang);
}
}
}
}
$data = LocalesLoader::getLocalesFrom('view~dictionnary', $lang);
$rep->content = 'var lizDict = '.json_encode($data).';';

return $rep;
Expand All @@ -68,23 +59,10 @@ public function getDictionary()
$lang = $this->param('lang');

if (!$lang) {
$lang = jLocale::getCurrentLang().'_'.jLocale::getCurrentCountry();
$lang = jLocale::getCurrentLocale();
}

$data = array();
$path = jApp::appPath().'modules/view/locales/'.$lang.'/'.$property.'.UTF-8.properties';
if (file_exists($path)) {
$lines = file($path);
foreach ($lines as $lineNumber => $lineContent) {
if (!empty($lineContent) and $lineContent != '\n') {
$exp = explode('=', trim($lineContent));
if (!empty($exp[0])) {
$data[$exp[0]] = jLocale::get('view~dictionnary.'.$exp[0], null, $lang);
}
}
}
}
$rep->data = $data;
$rep->data = LocalesLoader::getLocalesFrom('view~'.$property, $lang);

return $rep;
}
Expand Down
Loading

0 comments on commit 7ae4a94

Please sign in to comment.