forked from Wavyzz/dolibarr
Enhance multilangue support in website module
This commit is contained in:
@@ -59,16 +59,17 @@ class FormAdmin
|
||||
* @param int $forcecombo Force to use combo box (so no ajax beautify effect)
|
||||
* @param int $multiselect Make the combo a multiselect
|
||||
* @param array $onlykeys Show only the following keys (opposite of $filter)
|
||||
* @param int $mainlangonly 1=Show only main languages ('fr_FR' no' fr_BE', 'es_ES' not 'es_MX', ...)
|
||||
* @return string Return HTML select string with list of languages
|
||||
*/
|
||||
public function select_language($selected = '', $htmlname = 'lang_id', $showauto = 0, $filter = null, $showempty = '', $showwarning = 0, $disabled = 0, $morecss = '', $showcode = 0, $forcecombo = 0, $multiselect = 0, $onlykeys = array())
|
||||
public function select_language($selected = '', $htmlname = 'lang_id', $showauto = 0, $filter = null, $showempty = '', $showwarning = 0, $disabled = 0, $morecss = '', $showcode = 0, $forcecombo = 0, $multiselect = 0, $onlykeys = array(), $mainlangonly = 0)
|
||||
{
|
||||
// phpcs:enable
|
||||
global $conf, $langs;
|
||||
|
||||
if (!empty($conf->global->MAIN_DEFAULT_LANGUAGE_FILTER)) $filter[$conf->global->MAIN_DEFAULT_LANGUAGE_FILTER] = 1;
|
||||
|
||||
$langs_available=$langs->get_available_languages(DOL_DOCUMENT_ROOT, 12);
|
||||
$langs_available=$langs->get_available_languages(DOL_DOCUMENT_ROOT, 12, 0, $mainlangonly);
|
||||
|
||||
$out='';
|
||||
|
||||
@@ -95,21 +96,28 @@ class FormAdmin
|
||||
{
|
||||
$valuetoshow=$value;
|
||||
if ($showcode == 1) $valuetoshow=$key.' - '.$value;
|
||||
if ($showcode == 2) $valuetoshow=$value.' ('.$key.')';
|
||||
if ($showcode == 2) {
|
||||
if ($mainlangonly) $valuetoshow=$value.' ('.preg_replace('/[_-].*$/', '', $key).')';
|
||||
else $valuetoshow=$value.' ('.$key.')';
|
||||
}
|
||||
|
||||
if ($filter && is_array($filter) && array_key_exists($key, $filter)) {
|
||||
$keytouse = $key;
|
||||
if ($mainlangonly) $keytouse = preg_replace('/[_-].*$/', '', $key);
|
||||
|
||||
if ($filter && is_array($filter) && array_key_exists($keytouse, $filter)) {
|
||||
continue;
|
||||
}
|
||||
if ($onlykeys && is_array($onlykeys) && ! array_key_exists($key, $onlykeys)) {
|
||||
if ($onlykeys && is_array($onlykeys) && ! array_key_exists($keytouse, $onlykeys)) {
|
||||
continue;
|
||||
}
|
||||
if ($selected == $key)
|
||||
|
||||
if ($selected == $keytouse)
|
||||
{
|
||||
$out.= '<option value="'.$key.'" selected>'.$valuetoshow.'</option>';
|
||||
$out.= '<option value="'.$keytouse.'" selected>'.$valuetoshow.'</option>';
|
||||
}
|
||||
else
|
||||
{
|
||||
$out.= '<option value="'.$key.'">'.$valuetoshow.'</option>';
|
||||
$out.= '<option value="'.$keytouse.'">'.$valuetoshow.'</option>';
|
||||
}
|
||||
}
|
||||
$out.= '</select>';
|
||||
|
||||
@@ -269,7 +269,15 @@ class FormWebsite
|
||||
|
||||
$valueforoption = '<span class="opacitymedium">['.$valpage->type_container.' '.sprintf("%03d", $valpage->id).']</span> ';
|
||||
$valueforoption .= $valpage->pageurl.' - '.$valpage->title;
|
||||
if ($website->fk_default_home && $key == $website->fk_default_home) $valueforoption .= ' <span class="opacitymedium">('.$langs->trans("HomePage").')</span>';
|
||||
if ($website->otherlang) { // If there is alternative lang for this web site, we show the language code
|
||||
if ($valpage->lang) {
|
||||
$valueforoption .= ' <span class="opacitymedium">('.$valpage->lang.')</span>';
|
||||
}
|
||||
}
|
||||
if ($website->fk_default_home && $key == $website->fk_default_home) {
|
||||
//$valueforoption .= ' <span class="opacitymedium">('.$langs->trans("HomePage").')</span>';
|
||||
$valueforoption .= ' <span class="opacitymedium fa fa-home"></span>';
|
||||
}
|
||||
|
||||
$out .= '<option value="'.$key.'"';
|
||||
if ($pageid > 0 && $pageid == $key) $out .= ' selected'; // To preselect a value
|
||||
|
||||
@@ -764,28 +764,37 @@ class Translate
|
||||
* @param string $langdir Directory to scan
|
||||
* @param integer $maxlength Max length for each value in combo box (will be truncated)
|
||||
* @param int $usecode 1=Show code instead of country name for language variant, 2=Show only code
|
||||
* @param int $mainlangonly 1=Show only main languages ('fr_FR' no' fr_BE', 'es_ES' not 'es_MX', ...)
|
||||
* @return array List of languages
|
||||
*/
|
||||
public function get_available_languages($langdir = DOL_DOCUMENT_ROOT, $maxlength = 0, $usecode = 0)
|
||||
public function get_available_languages($langdir = DOL_DOCUMENT_ROOT, $maxlength = 0, $usecode = 0, $mainlangonly = 0)
|
||||
{
|
||||
// phpcs:enable
|
||||
global $conf;
|
||||
|
||||
$this->load("languages");
|
||||
|
||||
// We scan directory langs to detect available languages
|
||||
$handle = opendir($langdir."/langs");
|
||||
$langs_available = array();
|
||||
while ($dir = trim(readdir($handle)))
|
||||
{
|
||||
if (preg_match('/^[a-z]+_[A-Z]+/i', $dir))
|
||||
$regs = array();
|
||||
if (preg_match('/^([a-z]+)_([A-Z]+)/i', $dir, $regs))
|
||||
{
|
||||
$this->load("languages");
|
||||
|
||||
// We must keep only main languages
|
||||
if ($mainlangonly) {
|
||||
$arrayofspecialmainlanguages = array('en_US', 'sq_AL', 'ar_SA', 'eu_ES', 'bn_DB', 'bs_BA', 'ca_ES', 'zh_TW', 'cs_CZ', 'da_DK', 'et_EE', 'ka_GE', 'el_GR', 'he_IL', 'kn_IN', 'km_KH', 'ko_KR', 'lo_LA', 'nb_NO', 'fa_IR', 'sr_RS', 'sl_SI', 'uk_UA', 'vi_VN');
|
||||
if (strtolower($regs[1]) != strtolower($regs[2]) && ! in_array($dir, $arrayofspecialmainlanguages)) continue;
|
||||
}
|
||||
// We must keep only languages into MAIN_LANGUAGES_ALLOWED
|
||||
if (!empty($conf->global->MAIN_LANGUAGES_ALLOWED) && !in_array($dir, explode(',', $conf->global->MAIN_LANGUAGES_ALLOWED))) continue;
|
||||
|
||||
if ($usecode == 2)
|
||||
{
|
||||
$langs_available[$dir] = $dir;
|
||||
}
|
||||
|
||||
if ($usecode == 1 || !empty($conf->global->MAIN_SHOW_LANGUAGE_CODE))
|
||||
{
|
||||
$langs_available[$dir] = $dir.': '.dol_trunc($this->trans('Language_'.$dir), $maxlength);
|
||||
@@ -794,6 +803,9 @@ class Translate
|
||||
{
|
||||
$langs_available[$dir] = $this->trans('Language_'.$dir);
|
||||
}
|
||||
if ($mainlangonly) {
|
||||
$langs_available[$dir] = str_replace(' (United States)', '', $langs_available[$dir]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $langs_available;
|
||||
|
||||
Reference in New Issue
Block a user