1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-07 09:16:55 +02:00

[ticket/17191] Inject variables from composer.json when loading language files

PHPBB3-17191
This commit is contained in:
Marc Alexander
2023-10-04 21:16:54 +02:00
parent 4baa3e778d
commit dc9ef40669
10 changed files with 69 additions and 100 deletions

View File

@@ -71,29 +71,15 @@ class language
*/
protected $loader;
/**
* @var language_file_helper
*/
protected $lang_helper;
/**
* @var config
*/
protected $config;
/**
* Constructor
*
* @param \phpbb\language\language_file_loader $loader Language file loader
* @param language_file_helper $lang_helper
* @param array|null $common_modules Array of common language modules to load (optional)
* @param config $config The config
*/
public function __construct(language_file_loader $loader, language_file_helper $lang_helper, config $config, $common_modules = null)
public function __construct(language_file_loader $loader, $common_modules = null)
{
$this->loader = $loader;
$this->lang_helper = $lang_helper;
$this->config = $config;
// Set up default information
$this->user_language = null;
@@ -399,10 +385,27 @@ class language
$this->load_core_file($lang_file);
}
$this->inject_default_variables();
$this->common_language_files_loaded = true;
}
}
/**
* Inject default values based on composer.json
*
* @return void
*/
protected function inject_default_variables(): void
{
$lang_values = $this->loader->get_composer_lang_values($this->language_fallback);
$this->lang['DIRECTION'] = $lang_values['direction'] ?? 'ltr';
$this->lang['USER_LANG'] = $lang_values['user_lang'] ?? 'en-gb';
$this->lang['PLURAL_RULE'] = $lang_values['plural_rule'] ?? 1;
$this->lang['RECAPTCHA_LANG'] = $lang_values['recaptcha_lang'] ?? 'en-GB';
}
/**
* Determine which plural form we should use.
*
@@ -418,13 +421,8 @@ class language
*/
public function get_plural_form($number, $force_rule = false)
{
global $user;
// Grab the users lang plural rule
$plural_rule_content = $this->lang_helper->get_lang_key_value('plural_rule', $user->data['user_lang']);
$number = (int) $number;
$plural_rule = ($force_rule !== false) ? $force_rule : ((isset($plural_rule_content)) ? $plural_rule_content : 1);
$plural_rule = ($force_rule !== false) ? $force_rule : ((isset($this->lang['PLURAL_RULE'])) ? $this->lang['PLURAL_RULE'] : 1);
/**
* The following plural rules are based on a list published by the Mozilla Developer Network

View File

@@ -28,22 +28,15 @@ class language_file_helper
*/
protected $phpbb_root_path;
/**
* @var config
*/
protected $config;
/**
* Constructor
*
* @param string $phpbb_root_path Path to phpBB's root
* @param config $config The config
*
*/
public function __construct(string $phpbb_root_path, config $config)
public function __construct(string $phpbb_root_path)
{
$this->phpbb_root_path = $phpbb_root_path;
$this->config = $config;
}
/**
@@ -76,26 +69,6 @@ class language_file_helper
return $available_languages;
}
/**
* Return by given lang key its composer.json value
*
* @return string
*
*/
public function get_lang_key_value($lang_key, $user_lang_db) : string
{
$available_languages = $this->get_available_languages();
foreach ($available_languages as $key => $value)
{
$available_languages[$value['iso']] = $value;
unset($available_languages[$key]);
}
$board_default_lang = $this->config['default_lang'];
$user_lang_db = $user_lang_db ?? $board_default_lang;
return $available_languages[$user_lang_db][$lang_key];
}
/**
* Collect some data from the composer.json file
*

View File

@@ -186,6 +186,39 @@ class language_file_loader
throw new language_file_not_found('Language file ' . $language_file_path . ' couldn\'t be opened.');
}
/**
* Get language values from composer.json files
*
* @param array|string $locales
* @return array
*/
public function get_composer_lang_values(array|string $locales): array
{
if (!is_array($locales))
{
$locales = [$locales];
}
$file_helper = new language_file_helper($this->phpbb_root_path);
$composer_lang_vars = $file_helper->get_available_languages();
foreach ($composer_lang_vars as $key => $value)
{
$composer_lang_vars[$value['iso']] = $value;
unset($composer_lang_vars[$key]);
}
foreach ($locales as $locale)
{
if (isset($composer_lang_vars[$locale]))
{
return $composer_lang_vars[$locale];
}
}
return count($composer_lang_vars) ? array_shift($composer_lang_vars) : [];
}
/**
* Loads language file
*