Commit ae63b070 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1572377: Selecting correct lang file to use with select2



behatnotneeded - unable to test other languages

Change-Id: I417b1b1bd8fd8dcddcd1c7277d2da83d2c3c6f8e
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
(cherry picked from commit 8d58e3a6)
parent 17211abf
......@@ -64,6 +64,8 @@ defined('INTERNAL') || die();
function pieform_element_autocomplete(Pieform $form, $element) {
global $USER;
$wwwroot = get_config('wwwroot');
$lang = pieform_element_autocomplete_language();
$smarty = smarty_core();
$smarty->left_delimiter = '{{';
......@@ -108,6 +110,7 @@ function pieform_element_autocomplete(Pieform $form, $element) {
$smarty->assign('allowclear', empty($element['allowclear']) ? 'false' : 'true');
$smarty->assign('disabled', !empty($element['disabled']) ? 'true' : 'false');
$smarty->assign('ajaxurl', $element['ajaxurl']);
$smarty->assign('language', $lang);
$smarty->assign('sesskey', $USER->get('sesskey'));
$smarty->assign('hint', empty($element['hint']) ? get_string('defaulthint') : $element['hint']);
$smarty->assign('extraparams', $extraparams);
......@@ -120,35 +123,47 @@ function pieform_element_autocomplete(Pieform $form, $element) {
}
/**
* Returns code to go in <head> for the given autocomplete instance
* Returns the current language that the user is viewing the form with
* or 'en' if a corresponding select2 lang file is not found.
*
* @param array $element The element to get <head> code for
* @return array An array of HTML elements to go in the <head>
* @return string $langstr A valid lang string
*/
function pieform_element_autocomplete_get_headdata($element) {
function pieform_element_autocomplete_language() {
global $THEME;
$cssfile = $THEME->get_url('style/select2.css');
// Add language file if required.
$lang = current_language();
// Replace '_' with '-' which is used in select2.
$lang = str_replace('_', '-', substr($lang, 0, ((substr_count($lang, '_') > 0) ? 5 : 2)));
$langfile = '';
if ($lang != 'en' && file_exists(get_config('docroot') . "js/select2/select2_locale_{$lang}.js")) {
$langfile = '<script type="application/javascript" src="' .
get_config('wwwroot') . "js/select2/select2_locale_{$lang}.js" .
'"></script>';
if ($lang != 'en' && file_exists(get_config('docroot') . "js/select2/i18n/{$lang}.js")) {
return $lang;
}
else {
// Try parent language pack, which, for example, would be 'pt' for 'pt-BR'.
$lang = substr($lang, 0, 2);
if ($lang != 'en' && file_exists(get_config('docroot') . "js/select2/select2_locale_{$lang}.js")) {
$langfile = '<script type="application/javascript" src="' .
get_config('wwwroot') . "js/select2/select2_locale_{$lang}.js" .
'"></script>';
if ($lang != 'en' && file_exists(get_config('docroot') . "js/select2/i18n/{$lang}.js")) {
return $lang;
}
}
return 'en';
}
/**
* Returns code to go in <head> for the given autocomplete instance
*
* @param array $element The element to get <head> code for
* @return array An array of HTML elements to go in the <head>
*/
function pieform_element_autocomplete_get_headdata($element) {
global $THEME;
$cssfile = $THEME->get_url('style/select2.css');
$lang = pieform_element_autocomplete_language();
$langfile = '';
if ($lang != 'en') {
$langfile = '<script type="application/javascript" src="' .
get_config('wwwroot') . "js/select2/i18n/{$lang}.js" .
'"></script>';
}
$r = <<<JS
<link rel="stylesheet" href="{$cssfile}" />
{$langfile}
......
......@@ -34,6 +34,7 @@
};
}
},
language: "{{$language}}",
multiple: {{$multiple}},
width: "{{$width}}",
allowClear: {{$allowclear}},
......
......@@ -25,6 +25,7 @@
{foreach from=$JAVASCRIPT item=script}
<script type="application/javascript" src="{$script}"></script>
{/foreach}
<script type="application/javascript" src="{$WWWROOT}/js/select2/select2.full.js?v={$CACHEVERSION}"></script>
{if isset($INLINEJAVASCRIPT)}
<script type="application/javascript">
{$INLINEJAVASCRIPT|safe}
......@@ -41,7 +42,6 @@
<script type="application/javascript" src="{$WWWROOT}/js/javascript-templates/js/tmpl.min.js?v={$CACHEVERSION}"></script>
<script type="application/javascript" src="{$WWWROOT}js/jquery.rating.js?v={$CACHEVERSION}"></script>
<script type="application/javascript" src="{theme_url filename='js/masonry.min.js'}?v={$CACHEVERSION}"></script>
<script type="application/javascript" src="{$WWWROOT}/js/select2/select2.full.js?v={$CACHEVERSION}"></script>
<script type="application/javascript" src="{theme_url filename='js/pieform.js'}?v={$CACHEVERSION}"></script>
<script type="application/javascript" src="{theme_url filename='js/formtabs.js'}?v={$CACHEVERSION}"></script>
<script type="application/javascript" src="{theme_url filename='js/filebrowser.js'}?v={$CACHEVERSION}"></script>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment