[ticket/13740] Allow language change in the installer

PHPBB3-13740
This commit is contained in:
Mate Bartus 2015-07-22 03:16:16 +02:00
parent 11642a5f94
commit 97d08d6f56
7 changed files with 212 additions and 95 deletions

View file

@ -15,10 +15,14 @@
<h1>{L_INSTALL_PANEL}</h1> <h1>{L_INSTALL_PANEL}</h1>
<p id="skip"><a href="#acp">{L_SKIP}</a></p> <p id="skip"><a href="#acp">{L_SKIP}</a></p>
<!-- IF S_LANG_SELECT --> <!-- IF S_LANG_SELECT -->
<form method="post" action="#"> <form method="post" action="#" id="language_selector">
<fieldset class="nobg"> <fieldset class="nobg">
<label for="language">{L_SELECT_LANG}{L_COLON}</label> <label for="language">{L_SELECT_LANG}{L_COLON}</label>
{S_LANG_SELECT} <select id="language" name="language">
<!-- BEGIN language_select_item -->
<option value="{language_select_item.VALUE}"<!-- IF language_select_item.SELECTED --> selected="selected"<!-- ENDIF -->>{language_select_item.NAME}</option>
<!-- END language_select_item -->
</select>
<input class="button1" type="submit" id="change_lang" name="change_lang" value="{L_CHANGE}" /> <input class="button1" type="submit" id="change_lang" name="change_lang" value="{L_CHANGE}" />
</fieldset> </fieldset>
</form> </form>

View file

@ -330,6 +330,9 @@
xhReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhReq.send(getFormFields($form, $submitBtn)); xhReq.send(getFormFields($form, $submitBtn));
// Disable language selector
$('#language_selector :input, label').css('display', 'none');
// Clear content // Clear content
setupAjaxLayout(); setupAjaxLayout();
$('#loading_indicator').css('display', 'block'); $('#loading_indicator').css('display', 'block');

View file

@ -15,6 +15,7 @@ services:
- @installer.navigation.provider - @installer.navigation.provider
- @template - @template
- @path_helper - @path_helper
- @request
- @symfony_request - @symfony_request
- @router - @router
- %core.root_path% - %core.root_path%

View file

@ -17,10 +17,13 @@ use phpbb\install\helper\navigation\navigation_provider;
use phpbb\language\language; use phpbb\language\language;
use phpbb\language\language_file_helper; use phpbb\language\language_file_helper;
use phpbb\path_helper; use phpbb\path_helper;
use phpbb\request\request;
use phpbb\request\request_interface;
use phpbb\routing\router; use phpbb\routing\router;
use phpbb\symfony_request; use phpbb\symfony_request;
use phpbb\template\template; use phpbb\template\template;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
/** /**
* A duplicate of \phpbb\controller\helper * A duplicate of \phpbb\controller\helper
@ -35,6 +38,11 @@ class helper
*/ */
protected $language; protected $language;
/**
* @var bool|string
*/
protected $language_cookie;
/** /**
* @var \phpbb\language\language_file_helper * @var \phpbb\language\language_file_helper
*/ */
@ -55,6 +63,11 @@ class helper
*/ */
protected $path_helper; protected $path_helper;
/**
* @var \phpbb\request\request
*/
protected $phpbb_request;
/** /**
* @var \phpbb\symfony_request * @var \phpbb\symfony_request
*/ */
@ -83,120 +96,58 @@ class helper
* @param navigation_provider $nav * @param navigation_provider $nav
* @param template $template * @param template $template
* @param path_helper $path_helper * @param path_helper $path_helper
* @param request $phpbb_request
* @param symfony_request $request * @param symfony_request $request
* @param router $router * @param router $router
* @param string $phpbb_root_path * @param string $phpbb_root_path
*/ */
public function __construct(language $language, language_file_helper $lang_helper, navigation_provider $nav, template $template, path_helper $path_helper, symfony_request $request, router $router, $phpbb_root_path) public function __construct(language $language, language_file_helper $lang_helper, navigation_provider $nav, template $template, path_helper $path_helper, request $phpbb_request, symfony_request $request, router $router, $phpbb_root_path)
{ {
$this->language = $language; $this->language = $language;
$this->language_cookie = false;
$this->lang_helper = $lang_helper; $this->lang_helper = $lang_helper;
$this->navigation_provider = $nav; $this->navigation_provider = $nav;
$this->template = $template; $this->template = $template;
$this->path_helper = $path_helper; $this->path_helper = $path_helper;
$this->phpbb_request = $phpbb_request;
$this->request = $request; $this->request = $request;
$this->router = $router; $this->router = $router;
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
$this->phpbb_admin_path = $phpbb_root_path . 'adm/'; $this->phpbb_admin_path = $phpbb_root_path . 'adm/';
$this->handle_language_select();
} }
/** /**
* Automate setting up the page and creating the response object. * Automate setting up the page and creating the response object.
* *
* @param string $template_file The template handle to render * @param string $template_file The template handle to render
* @param string $page_title The title of the page to output * @param string $page_title The title of the page to output
* @param int $status_code The status code to be sent to the page header * @param bool $selected_language True to enable language selector it, false otherwise
* @param int $status_code The status code to be sent to the page header
* *
* @return Response object containing rendered page * @return Response object containing rendered page
*/ */
public function render($template_file, $page_title = '', $status_code = 200) public function render($template_file, $page_title = '', $selected_language = false, $status_code = 200)
{ {
$this->page_header($page_title); $this->page_header($page_title, $selected_language);
$this->template->set_filenames(array( $this->template->set_filenames(array(
'body' => $template_file, 'body' => $template_file,
)); ));
return new Response($this->template->assign_display('body'), $status_code); $response = new Response($this->template->assign_display('body'), $status_code);
}
/** // Set language cookie
* Set default template variables if ($this->language_cookie !== false)
*
* @param string $page_title
*/
protected function page_header($page_title)
{
$this->template->assign_vars(array(
'L_CHANGE' => $this->language->lang('CHANGE'),
'L_COLON' => $this->language->lang('COLON'),
'L_INSTALL_PANEL' => $this->language->lang('INSTALL_PANEL'),
'L_SELECT_LANG' => $this->language->lang('SELECT_LANG'),
'L_SKIP' => $this->language->lang('SKIP'),
'PAGE_TITLE' => $this->language->lang($page_title),
'T_IMAGE_PATH' => htmlspecialchars($this->phpbb_admin_path) . 'images/',
'T_JQUERY_LINK' => $this->path_helper->get_web_root_path() . 'assets/javascript/jquery.min.js',
'T_TEMPLATE_PATH' => $this->path_helper->get_web_root_path() . 'adm/style',
'T_ASSETS_PATH' => $this->path_helper->get_web_root_path() . 'assets/',
'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_USER_LANG' => $this->language->lang('USER_LANG'),
)
);
$this->render_navigation();
}
/**
* Render navigation
*/
protected function render_navigation()
{
// Get navigation items
$nav_array = $this->navigation_provider->get();
$nav_array = $this->sort_navigation_level($nav_array);
$active_main_menu = $this->get_active_main_menu($nav_array);
// Pass navigation to template
foreach ($nav_array as $key => $entry)
{ {
$this->template->assign_block_vars('t_block1', array( $cookie = new Cookie('lang', $this->language_cookie, time() + 3600);
'L_TITLE' => $this->language->lang($entry['label']), $response->headers->setCookie($cookie);
'S_SELECTED' => ($active_main_menu === $key),
'U_TITLE' => $this->route($entry['route']),
));
if (is_array($entry[0]) && $active_main_menu === $key) $this->language_cookie = false;
{
$entry[0] = $this->sort_navigation_level($entry[0]);
foreach ($entry[0] as $name => $sub_entry)
{
if (isset($sub_entry['stage']) && $sub_entry['stage'] === true)
{
$this->template->assign_block_vars('l_block2', array(
'L_TITLE' => $this->language->lang($sub_entry['label']),
'S_SELECTED' => (isset($sub_entry['selected']) && $sub_entry['selected'] === true),
'S_COMPLETE' => (isset($sub_entry['completed']) && $sub_entry['completed'] === true),
'STAGE_NAME' => $name,
));
}
else
{
$this->template->assign_block_vars('l_block1', array(
'L_TITLE' => $this->language->lang($sub_entry['label']),
'S_SELECTED' => (isset($sub_entry['route']) && $sub_entry['route'] === $this->request->get('_route')),
'U_TITLE' => $this->route($sub_entry['route']),
));
}
}
}
} }
return $response;
} }
/** /**
@ -214,12 +165,134 @@ class helper
} }
/** /**
* Render language select form * Handles language selector form
*/ */
protected function render_language_select() protected function handle_language_select()
{
$lang = null;
// Check if language form has been submited
$submit = $this->phpbb_request->variable('change_lang', '');
if (!empty($submit))
{
$lang = $this->phpbb_request->variable('language', '');
if (!empty($lang))
{
$this->language_cookie = $lang;
}
}
// Retrive language from cookie
$lang_cookie = $this->phpbb_request->variable('lang', '', false, request_interface::COOKIE);
if (empty($lang) && !empty($lang_cookie))
{
$lang = $lang_cookie;
$this->language_cookie = $lang;
}
$lang = (!empty($lang)) ? $lang : null;
$this->render_language_select($lang);
if ($lang !== null)
{
$this->language->set_user_language($lang, true);
}
}
/**
* Set default template variables
*
* @param string $page_title Title of the page
* @param bool $selected_language True to enable language selector it, false otherwise
*/
protected function page_header($page_title, $selected_language = false)
{
$this->template->assign_vars(array(
'L_CHANGE' => $this->language->lang('CHANGE'),
'L_COLON' => $this->language->lang('COLON'),
'L_INSTALL_PANEL' => $this->language->lang('INSTALL_PANEL'),
'L_SELECT_LANG' => $this->language->lang('SELECT_LANG'),
'L_SKIP' => $this->language->lang('SKIP'),
'PAGE_TITLE' => $this->language->lang($page_title),
'T_IMAGE_PATH' => htmlspecialchars($this->phpbb_admin_path) . 'images/',
'T_JQUERY_LINK' => $this->path_helper->get_web_root_path() . 'assets/javascript/jquery.min.js',
'T_TEMPLATE_PATH' => $this->path_helper->get_web_root_path() . 'adm/style',
'T_ASSETS_PATH' => $this->path_helper->get_web_root_path() . 'assets/',
'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->language->lang('USER_LANG'),
)
);
$this->render_navigation();
}
/**
* Render navigation
*/
protected function render_navigation()
{
// Get navigation items
$nav_array = $this->navigation_provider->get();
$nav_array = $this->sort_navigation_level($nav_array);
$active_main_menu = $this->get_active_main_menu($nav_array);
// Pass navigation to template
foreach ($nav_array as $key => $entry) {
$this->template->assign_block_vars('t_block1', array(
'L_TITLE' => $this->language->lang($entry['label']),
'S_SELECTED' => ($active_main_menu === $key),
'U_TITLE' => $this->route($entry['route']),
));
if (is_array($entry[0]) && $active_main_menu === $key) {
$entry[0] = $this->sort_navigation_level($entry[0]);
foreach ($entry[0] as $name => $sub_entry) {
if (isset($sub_entry['stage']) && $sub_entry['stage'] === true) {
$this->template->assign_block_vars('l_block2', array(
'L_TITLE' => $this->language->lang($sub_entry['label']),
'S_SELECTED' => (isset($sub_entry['selected']) && $sub_entry['selected'] === true),
'S_COMPLETE' => (isset($sub_entry['completed']) && $sub_entry['completed'] === true),
'STAGE_NAME' => $name,
));
} else {
$this->template->assign_block_vars('l_block1', array(
'L_TITLE' => $this->language->lang($sub_entry['label']),
'S_SELECTED' => (isset($sub_entry['route']) && $sub_entry['route'] === $this->request->get('_route')),
'U_TITLE' => $this->route($sub_entry['route']),
));
}
}
}
}
}
/**
* Render language select form
*
* @param string $selected_language
*/
protected function render_language_select($selected_language = null)
{ {
$langs = $this->lang_helper->get_available_languages(); $langs = $this->lang_helper->get_available_languages();
// @todo Implement language change option foreach ($langs as $lang)
{
$this->template->assign_block_vars('language_select_item', array(
'VALUE' => $lang['iso'],
'NAME' => $lang['local_name'],
'SELECTED' => ($lang['iso'] === $selected_language),
));
}
} }
/** /**

View file

@ -189,7 +189,7 @@ class install
'TITLE' => $this->language->lang('INSTALL_INTRO'), 'TITLE' => $this->language->lang('INSTALL_INTRO'),
'CONTENT' => $this->language->lang('INSTALL_INTRO_BODY'), 'CONTENT' => $this->language->lang('INSTALL_INTRO_BODY'),
)); ));
return $this->controller_helper->render('installer_install.html', 'INSTALL'); return $this->controller_helper->render('installer_install.html', 'INSTALL', true);
} }
// @todo: implement no js controller logic // @todo: implement no js controller logic

View file

@ -74,6 +74,6 @@ class installer_index
'BODY' => $body, 'BODY' => $body,
)); ));
return $this->helper->render('install_main.html', $title); return $this->helper->render('install_main.html', $title, true);
} }
} }

View file

@ -109,25 +109,27 @@ class language
/** /**
* Function to set user's language to display. * Function to set user's language to display.
* *
* @param string $user_lang_iso ISO code of the User's language * @param string $user_lang_iso ISO code of the User's language
* @param bool $reload Whether or not to reload language files
*/ */
public function set_user_language($user_lang_iso) public function set_user_language($user_lang_iso, $reload = false)
{ {
$this->user_language = $user_lang_iso; $this->user_language = $user_lang_iso;
$this->set_fallback_array(); $this->set_fallback_array($reload);
} }
/** /**
* Function to set the board's default language to display. * Function to set the board's default language to display.
* *
* @param string $default_lang_iso ISO code of the board's default language * @param string $default_lang_iso ISO code of the board's default language
* @param bool $reload Whether or not to reload language files
*/ */
public function set_default_language($default_lang_iso) public function set_default_language($default_lang_iso, $reload = false)
{ {
$this->default_language = $default_lang_iso; $this->default_language = $default_lang_iso;
$this->set_fallback_array(); $this->set_fallback_array($reload);
} }
/** /**
@ -508,9 +510,11 @@ class language
/** /**
* Returns language fallback data * Returns language fallback data
* *
* @param bool $reload Whether or not to reload language files
*
* @return array * @return array
*/ */
protected function set_fallback_array() protected function set_fallback_array($reload = false)
{ {
$fallback_array = array(); $fallback_array = array();
@ -527,6 +531,11 @@ class language
$fallback_array[] = self::FALLBACK_LANGUAGE; $fallback_array[] = self::FALLBACK_LANGUAGE;
$this->language_fallback = $fallback_array; $this->language_fallback = $fallback_array;
if ($reload)
{
$this->reload_language_files();
}
} }
/** /**
@ -563,4 +572,31 @@ class language
$this->loader->load_extension($extension_name, $component, $this->language_fallback, $this->lang); $this->loader->load_extension($extension_name, $component, $this->language_fallback, $this->lang);
$this->loaded_language_sets['ext'][$extension_name][$component] = true; $this->loaded_language_sets['ext'][$extension_name][$component] = true;
} }
/**
* Reload language files
*/
protected function reload_language_files()
{
$loaded_files = $this->loaded_language_sets;
$this->loaded_language_sets = array(
'core' => array(),
'ext' => array(),
);
// Reload core files
foreach ($loaded_files['core'] as $component => $value)
{
$this->load_core_file($component);
}
// Reload extension files
foreach ($loaded_files['ext'] as $ext_name => $ext_info)
{
foreach ($ext_info as $ext_component => $value)
{
$this->load_extension($ext_name, $ext_component);
}
}
}
} }