1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-07 16:15:22 +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
No known key found for this signature in database
GPG Key ID: 50E0D2423696F995
10 changed files with 69 additions and 100 deletions

View File

@ -14,7 +14,6 @@ services:
- '@event_dispatcher'
- '@language'
- '%core.php_ext%'
- '@language.helper.language_file'
feed.helper:
class: phpbb\feed\helper

View File

@ -3,14 +3,11 @@ services:
class: phpbb\language\language_file_helper
arguments:
- '%core.root_path%'
- '@config'
language:
class: phpbb\language\language
arguments:
- '@language.loader'
- '@language.helper.language_file'
- '@config'
language.loader_abstract:
abstract: true

View File

@ -3824,15 +3824,7 @@ function page_header($page_title = '', $display_online_list = false, $item_id =
$web_path = $phpbb_path_helper->get_web_root_path();
// Send a proper content-language to the output
// Get the language helper
/* @var $language_helper \phpbb\language\language_file_helper */
$language_file_helper = $phpbb_container->get('language.helper.language_file');
// Grab the users lang direction and store it for later use
$direction = $language_file_helper->get_lang_key_value('direction', $user->data['user_lang']);
// Get the user_lang string
$user_lang = $language_file_helper->get_lang_key_value('user_lang', $user->data['user_lang']);
$user_lang = $user->lang('USER_LANG');
if (strpos($user_lang, '-x-') !== false)
{
$user_lang = substr($user_lang, 0, strpos($user_lang, '-x-'));
@ -3938,7 +3930,7 @@ function page_header($page_title = '', $display_online_list = false, $item_id =
'L_INDEX' => ($config['board_index_text'] !== '') ? $config['board_index_text'] : $user->lang['FORUM_INDEX'],
'L_SITE_HOME' => ($config['site_home_text'] !== '') ? $config['site_home_text'] : $user->lang['HOME'],
'L_ONLINE_EXPLAIN' => $l_online_time,
'L_RECAPTCHA_LANG' => $language_file_helper->get_lang_key_value('recaptcha_lang', $user->data['user_lang']),
'L_RECAPTCHA_LANG' => $user->lang('RECAPTCHA_LANG'),
'U_PRIVATEMSGS' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox'),
'U_RETURN_INBOX' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox'),
@ -3979,9 +3971,9 @@ function page_header($page_title = '', $display_online_list = false, $item_id =
'S_USER_LANG' => $user_lang,
'S_USER_BROWSER' => (isset($user->data['session_browser'])) ? $user->data['session_browser'] : $user->lang['UNKNOWN_BROWSER'],
'S_USERNAME' => $user->data['username'],
'S_CONTENT_DIRECTION' => $direction,
'S_CONTENT_FLOW_BEGIN' => ($direction == 'ltr') ? 'left' : 'right',
'S_CONTENT_FLOW_END' => ($direction == 'ltr') ? 'right' : 'left',
'S_CONTENT_DIRECTION' => $user->lang('DIRECTION'),
'S_CONTENT_FLOW_BEGIN' => ($user->lang('DIRECTION') == 'ltr') ? 'left' : 'right',
'S_CONTENT_FLOW_END' => ($user->lang('DIRECTION') == 'ltr') ? 'right' : 'left',
'S_CONTENT_ENCODING' => 'UTF-8',
'S_TIMEZONE' => sprintf($user->lang['ALL_TIMES'], $timezone_offset, $timezone_name),
'S_DISPLAY_ONLINE_LIST' => ($l_online_time) ? 1 : 0,

View File

@ -69,16 +69,6 @@ function adm_page_header($page_title)
$phpbb_version_parts = explode('.', PHPBB_VERSION, 3);
$phpbb_major = $phpbb_version_parts[0] . '.' . $phpbb_version_parts[1];
// Get the language helper
/* @var $language_helper \phpbb\language\language_file_helper */
$language_file_helper = $phpbb_container->get('language.helper.language_file');
// Grab the users lang direction and store it for later use
$direction = $language_file_helper->get_lang_key_value('direction', $user->data['user_lang']);
// Get the user_lang string
$user_lang = $language_file_helper->get_lang_key_value('user_lang', $user->data['user_lang']);
$template->assign_vars(array(
'PAGE_TITLE' => $page_title,
'USERNAME' => $user->data['username'],
@ -119,11 +109,11 @@ function adm_page_header($page_title)
'ICON_SYNC_DISABLED' => '<i class="icon acp-icon acp-icon-disabled fa-refresh fa-fw" title="' . $user->lang('RESYNC') . '"></i>',
'S_USER_ID' => $user->data['user_id'],
'S_USER_LANG' => $user_lang,
'S_CONTENT_DIRECTION' => $direction,
'S_USER_LANG' => $user->lang('USER_LANG'),
'S_CONTENT_DIRECTION' => $user->lang('DIRECTION'),
'S_CONTENT_ENCODING' => 'UTF-8',
'S_CONTENT_FLOW_BEGIN' => ($direction == 'ltr') ? 'left' : 'right',
'S_CONTENT_FLOW_END' => ($direction == 'ltr') ? 'right' : 'left',
'S_CONTENT_FLOW_BEGIN' => ($user->lang('DIRECTION') == 'ltr') ? 'left' : 'right',
'S_CONTENT_FLOW_END' => ($user->lang('DIRECTION') == 'ltr') ? 'right' : 'left',
'CONTAINER_EXCEPTION' => $phpbb_container->hasParameter('container_exception') ? $phpbb_container->getParameter('container_exception') : false,
));

View File

@ -23,7 +23,6 @@ use phpbb\feed\exception\feed_unavailable_exception;
use phpbb\feed\exception\unauthorized_exception;
use phpbb\feed\helper as feed_helper;
use phpbb\controller\helper as controller_helper;
use phpbb\language\language_file_helper;
use phpbb\symfony_request;
use phpbb\user;
use phpbb\language\language;
@ -94,11 +93,6 @@ class feed
*/
protected $language;
/**
* @var language_file_helper
*/
protected $lang_helper;
/**
* Constructor
*
@ -114,9 +108,8 @@ class feed
* @param dispatcher_interface $phpbb_dispatcher
* @param language $language
* @param string $php_ext
* @param language_file_helper $lang_helper
*/
public function __construct(Environment $twig, symfony_request $request, controller_helper $controller_helper, config $config, driver_interface $db, ContainerInterface $container, feed_helper $feed_helper, user $user, auth $auth, dispatcher_interface $phpbb_dispatcher, language $language, $php_ext, language_file_helper $lang_helper)
public function __construct(Environment $twig, symfony_request $request, controller_helper $controller_helper, config $config, driver_interface $db, ContainerInterface $container, feed_helper $feed_helper, user $user, auth $auth, dispatcher_interface $phpbb_dispatcher, language $language, $php_ext)
{
$this->request = $request;
$this->controller_helper = $controller_helper;
@ -130,7 +123,6 @@ class feed
$this->template = $twig;
$this->language = $language;
$this->phpbb_dispatcher = $phpbb_dispatcher;
$this->lang_helper = $lang_helper;
}
/**
@ -395,7 +387,7 @@ class feed
'FEED_TITLE' => $this->config['sitename'],
'FEED_SUBTITLE' => $this->config['site_desc'],
'FEED_UPDATED' => $this->feed_helper->format_date($feed_updated_time),
'FEED_LANG' => $this->lang_helper->get_lang_key_value('user_lang', $this->user->data['user_lang']),
'FEED_LANG' => $this->language->lang('USER_LANG'),
'FEED_AUTHOR' => $this->config['sitename'],
// Feed entries

View File

@ -253,17 +253,12 @@ class helper
*/
protected function page_header($page_title, $selected_language = false)
{
global $user;
// Path to templates
$paths = array($this->phpbb_root_path . 'install/update/new/adm/', $this->phpbb_admin_path);
$paths = array_filter($paths, 'is_dir');
$path = array_shift($paths);
$path = substr($path, strlen($this->phpbb_root_path));
// Grab the users lang direction and store it for later use
$direction = $this->lang_helper->get_lang_key_value('direction', $user->data['user_lang']);
$this->template->assign_vars(array(
'L_CHANGE' => $this->language->lang('CHANGE'),
'L_COLON' => $this->language->lang('COLON'),
@ -276,13 +271,13 @@ class helper
'T_TEMPLATE_PATH' => $this->path_helper->get_web_root_path() . $path . 'style',
'T_ASSETS_PATH' => $this->path_helper->get_web_root_path() . $path . '../assets',
'S_CONTENT_DIRECTION' => $direction,
'S_CONTENT_FLOW_BEGIN' => ($direction === 'ltr') ? 'left' : 'right',
'S_CONTENT_FLOW_END' => ($direction === 'ltr') ? 'right' : 'left',
'S_CONTENT_DIRECTION' => $this->language->lang('DIRECTION'),
'S_CONTENT_FLOW_BEGIN' => ($this->language->lang('DIRECTION') === 'ltr') ? 'left' : 'right',
'S_CONTENT_FLOW_END' => ($this->language->lang('DIRECTION') === 'ltr') ? 'right' : 'left',
'S_CONTENT_ENCODING' => 'UTF-8',
'S_LANG_SELECT' => $selected_language,
'S_USER_LANG' => $this->lang_helper->get_lang_key_value('user_lang', $user->data['user_lang']),
'S_USER_LANG' => $this->language->lang('USER_LANG'),
));
$this->render_navigation();

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
*

View File

@ -98,7 +98,7 @@ class phpbb_attachment_upload_test extends \phpbb_database_test_case
$this->request = $this->createMock('\phpbb\request\request');
$this->filesystem = new \phpbb\filesystem\filesystem();
$this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx), new language_file_helper($phpbb_root_path, $config), $config);
$this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
$this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper;
$guessers = array(
new \Symfony\Component\Mime\FileinfoMimeTypeGuesser(),