diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 561e99a139..453e1d3150 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -3849,7 +3849,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 - $user_lang = $user->lang['USER_LANG']; + $user_lang = $user->lang('USER_LANG'); if (strpos($user_lang, '-x-') !== false) { $user_lang = substr($user_lang, 0, strpos($user_lang, '-x-')); @@ -3955,6 +3955,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' => $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'), @@ -3995,9 +3996,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' => $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_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, diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index 26d16aaae8..14eda9d9b6 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -109,11 +109,11 @@ function adm_page_header($page_title) 'ICON_SYNC_DISABLED' => '', 'S_USER_ID' => $user->data['user_id'], - 'S_USER_LANG' => $user->lang['USER_LANG'], - 'S_CONTENT_DIRECTION' => $user->lang['DIRECTION'], + 'S_USER_LANG' => $user->lang('USER_LANG'), + 'S_CONTENT_DIRECTION' => $user->lang('DIRECTION'), 'S_CONTENT_ENCODING' => 'UTF-8', - 'S_CONTENT_FLOW_BEGIN' => ($user->lang['DIRECTION'] == 'ltr') ? 'left' : 'right', - 'S_CONTENT_FLOW_END' => ($user->lang['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, )); diff --git a/phpBB/language/en/captcha_recaptcha.php b/phpBB/language/en/captcha_recaptcha.php index b76ae56ac0..513e47d3b4 100644 --- a/phpBB/language/en/captcha_recaptcha.php +++ b/phpBB/language/en/captcha_recaptcha.php @@ -37,10 +37,6 @@ if (empty($lang) || !is_array($lang)) // in a url you again do not need to specify an order e.g., 'Click %sHERE%s' is fine $lang = array_merge($lang, [ - // Find the language/country code on https://developers.google.com/recaptcha/docs/language - // If no code exists for your language you can use "en" or leave the string empty - 'RECAPTCHA_LANG' => 'en-GB', - 'CAPTCHA_RECAPTCHA' => 'reCaptcha v2', 'CAPTCHA_RECAPTCHA_V3' => 'reCaptcha v3', diff --git a/phpBB/language/en/common.php b/phpBB/language/en/common.php index 4266693440..18de2a0d2c 100644 --- a/phpBB/language/en/common.php +++ b/phpBB/language/en/common.php @@ -42,15 +42,8 @@ if (empty($lang) || !is_array($lang)) $lang = array_merge($lang, array( 'TRANSLATION_INFO' => '', - 'DIRECTION' => 'ltr', 'DATE_FORMAT' => '|d M Y|', // 01 Jan 2007 (with Relative days enabled) 'DATETIME_FORMAT' => '|d M Y, H:i|', // 01 Jan 2007, 13:37 (with Relative days enabled) - 'USER_LANG' => 'en-gb', - - // You can define different rules for the determination of plural forms here. - // See https://area51.phpbb.com/docs/dev/3.3.x/language/plurals.html for more information - // or ask the translation manager for help. - 'PLURAL_RULE' => 1, '1_DAY' => '1 day', '1_MONTH' => '1 month', diff --git a/phpBB/language/en/composer.json b/phpBB/language/en/composer.json index bbb2da4ae1..7402e5efa5 100644 --- a/phpBB/language/en/composer.json +++ b/phpBB/language/en/composer.json @@ -22,6 +22,10 @@ "language-iso": "en", "english-name": "British English", "local-name": "British English", - "phpbb-version": "4.0.0-a1-dev" + "phpbb-version": "4.0.0-a1-dev", + "direction": "ltr", + "user-lang": "en-gb", + "plural-rule": 1, + "recaptcha-lang": "en-GB" } } diff --git a/phpBB/phpbb/feed/controller/feed.php b/phpBB/phpbb/feed/controller/feed.php index d95cda5248..a7eb61ba6e 100644 --- a/phpBB/phpbb/feed/controller/feed.php +++ b/phpBB/phpbb/feed/controller/feed.php @@ -387,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->user->lang['USER_LANG'], + 'FEED_LANG' => $this->language->lang('USER_LANG'), 'FEED_AUTHOR' => $this->config['sitename'], // Feed entries diff --git a/phpBB/phpbb/language/language.php b/phpBB/phpbb/language/language.php index 6c59e02bba..dee8b58486 100644 --- a/phpBB/phpbb/language/language.php +++ b/phpBB/phpbb/language/language.php @@ -384,10 +384,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. * @@ -404,7 +421,7 @@ class language public function get_plural_form($number, $force_rule = false) { $number = (int) $number; - $plural_rule = ($force_rule !== false) ? $force_rule : ((isset($this->lang['PLURAL_RULE'])) ? $this->lang['PLURAL_RULE'] : 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 diff --git a/phpBB/phpbb/language/language_file_helper.php b/phpBB/phpbb/language/language_file_helper.php index e6966a3710..995c7b4712 100644 --- a/phpBB/phpbb/language/language_file_helper.php +++ b/phpBB/phpbb/language/language_file_helper.php @@ -30,7 +30,8 @@ class language_file_helper /** * Constructor * - * @param string $phpbb_root_path Path to phpBB's root + * @param string $phpbb_root_path Path to phpBB's root + * */ public function __construct(string $phpbb_root_path) { @@ -91,7 +92,7 @@ class language_file_helper */ protected function get_language_data_from_json(array $data) : array { - if (!isset($data['extra']['language-iso']) || !isset($data['extra']['english-name']) || !isset($data['extra']['local-name'])) + if (!isset($data['extra']['language-iso']) || !isset($data['extra']['english-name']) || !isset($data['extra']['local-name']) || !isset($data['extra']['direction']) || !isset($data['extra']['user-lang']) || !isset($data['extra']['plural-rule']) || !isset($data['extra']['recaptcha-lang'])) { throw new DomainException('INVALID_LANGUAGE_PACK'); } @@ -115,6 +116,10 @@ class language_file_helper 'author' => implode(', ', $authors), 'version' => $data['version'], 'phpbb_version' => $data['extra']['phpbb-version'], + 'direction' => $data['extra']['direction'], + 'user_lang' => $data['extra']['user-lang'], + 'plural_rule' => $data['extra']['plural-rule'], + 'recaptcha_lang'=> $data['extra']['recaptcha-lang'], ]; } } diff --git a/phpBB/phpbb/language/language_file_loader.php b/phpBB/phpbb/language/language_file_loader.php index 16dc2ab528..8c8abef7a5 100644 --- a/phpBB/phpbb/language/language_file_loader.php +++ b/phpBB/phpbb/language/language_file_loader.php @@ -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 *