Enhance multilangue support in website module

This commit is contained in:
Laurent Destailleur
2020-02-21 11:36:16 +01:00
parent 1f4b639ba0
commit 7909055ee8
12 changed files with 161 additions and 37 deletions

View File

@@ -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;