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;
|
||||
|
||||
@@ -239,15 +239,15 @@ function dolWebsiteOutput($content, $contenttype = 'html', $containerid = '')
|
||||
{
|
||||
global $website;
|
||||
|
||||
$content = str_replace('<link rel="stylesheet" href="/styles.css', '<link rel="stylesheet" href="styles.css', $content);
|
||||
|
||||
// Protect the link styles.css.php to any replacement that we make after.
|
||||
$content = str_replace('href="styles.css.php', 'href="!~!~!~styles.css.php', $content);
|
||||
$content = str_replace('href="http', 'href="!~!~!~http', $content);
|
||||
$content = str_replace('href="//', 'href="!~!~!~//', $content);
|
||||
$content = str_replace('src="viewimage.php', 'src="!~!~!~/viewimage.php', $content);
|
||||
$content = str_replace('src="/viewimage.php', 'src="!~!~!~/viewimage.php', $content);
|
||||
$content = str_replace(array('src="viewimage.php', 'src="/viewimage.php'), 'src="!~!~!~/viewimage.php', $content);
|
||||
$content = str_replace('src="'.DOL_URL_ROOT.'/viewimage.php', 'src="!~!~!~'.DOL_URL_ROOT.'/viewimage.php', $content);
|
||||
$content = str_replace('href="document.php', 'href="!~!~!~/document.php', $content);
|
||||
$content = str_replace('href="/document.php', 'href="!~!~!~/document.php', $content);
|
||||
$content = str_replace(array('href="document.php','href="/document.php'), 'href="!~!~!~/document.php', $content);
|
||||
$content = str_replace('href="'.DOL_URL_ROOT.'/document.php', 'href="!~!~!~'.DOL_URL_ROOT.'/document.php', $content);
|
||||
|
||||
// Replace relative link / with dolibarr URL: ...href="/"...
|
||||
@@ -286,6 +286,7 @@ function dolWebsiteOutput($content, $contenttype = 'html', $containerid = '')
|
||||
// Fix relative URL
|
||||
$content = str_replace('src="!~!~!~/viewimage.php', 'src="!~!~!~'.DOL_URL_ROOT.'/viewimage.php', $content);
|
||||
$content = str_replace('href="!~!~!~/document.php', 'href="!~!~!~'.DOL_URL_ROOT.'/document.php', $content);
|
||||
|
||||
// Remove the protection tag !~!~!~
|
||||
$content = str_replace('!~!~!~', '', $content);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ function dolSaveMasterFile($filemaster)
|
||||
* @param Website $object Object website
|
||||
* @param WebsitePage $objectpage Object websitepage
|
||||
* @return boolean True if OK
|
||||
* @see dolSavePageContent()
|
||||
*/
|
||||
function dolSavePageAlias($filealias, $object, $objectpage)
|
||||
{
|
||||
@@ -74,6 +75,24 @@ function dolSavePageAlias($filealias, $object, $objectpage)
|
||||
@chmod($filealias, octdec($conf->global->MAIN_UMASK));
|
||||
}
|
||||
|
||||
// Save also alias into language subdirectory if we have to
|
||||
if ($objectpage->lang && in_array($objectpage->lang, explode(',', $object->otherlang))) {
|
||||
$dirname = dirname($filealias);
|
||||
$filename = basename($filealias);
|
||||
$filealias = $dirname.'/'.$objectpage->lang.'/'.$filename;
|
||||
|
||||
$aliascontent = '<?php'."\n";
|
||||
$aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
|
||||
$aliascontent .= 'global $dolibarr_main_data_root;'."\n";
|
||||
$aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'../page'.$objectpage->id.'.tpl.php\'; ';
|
||||
$aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
|
||||
$aliascontent .= '?>'."\n";
|
||||
$result = file_put_contents($filealias, $aliascontent);
|
||||
if (!empty($conf->global->MAIN_UMASK)) {
|
||||
@chmod($filealias, octdec($conf->global->MAIN_UMASK));
|
||||
}
|
||||
}
|
||||
|
||||
return ($result ?true:false);
|
||||
}
|
||||
|
||||
@@ -85,6 +104,7 @@ function dolSavePageAlias($filealias, $object, $objectpage)
|
||||
* @param Website $object Object website
|
||||
* @param WebsitePage $objectpage Object websitepage
|
||||
* @return boolean True if OK
|
||||
* @see dolSavePageAlias()
|
||||
*/
|
||||
function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage)
|
||||
{
|
||||
@@ -101,7 +121,10 @@ function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage)
|
||||
$tplcontent = '';
|
||||
$tplcontent .= "<?php // BEGIN PHP\n";
|
||||
$tplcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
|
||||
$tplcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Not already loaded"."\n";
|
||||
$tplcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) {\n";
|
||||
$tplcontent .= ' $pathdepth = count(explode(\'/\', $_SERVER[\'SCRIPT_NAME\'])) - 2;'."\n";
|
||||
$tplcontent .= ' require_once ($pathdepth ? str_repeat(\'../\', $pathdepth) : \'./\').\'master.inc.php\';'."\n";
|
||||
$tplcontent .= "} // Not already loaded"."\n";
|
||||
$tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
|
||||
$tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
|
||||
$tplcontent .= "ob_start();\n";
|
||||
@@ -125,7 +148,7 @@ function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage)
|
||||
// Add translation reference (main language)
|
||||
if ($object->isMultiLang()) {
|
||||
// Add myself
|
||||
$tplcontent .= '<link rel="alternate" hreflang="'.$shortlangcode.'" href="'.($object->fk_default_home == $objectpage->id ? '/' : '/'.$objectpage->pageurl.'.php').'" />'."\n";
|
||||
$tplcontent .= '<link rel="alternate" hreflang="'.$shortlangcode.'" href="'.($object->fk_default_home == $objectpage->id ? '/' : '/'.$shortlangcode.'/'.$objectpage->pageurl.'.php').'" />'."\n";
|
||||
// Add page "translation of"
|
||||
$translationof = $objectpage->fk_page;
|
||||
if ($translationof) {
|
||||
@@ -135,7 +158,7 @@ function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage)
|
||||
$tmpshortlangcode = '';
|
||||
if ($tmppage->lang) $tmpshortlangcode = preg_replace('/[_-].*$/', '', $tmppage->lang); // en_US or en-US -> en
|
||||
if ($tmpshortlangcode != $shortlangcode) {
|
||||
$tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $tmppage->id ? '/' : '/'.$tmppage->pageurl.'.php').'" />'."\n";
|
||||
$tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $tmppage->id ? '/' : '/'.$tmpshortlangcode.'/'.$tmppage->pageurl.'.php').'" />'."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,7 +175,7 @@ function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage)
|
||||
$tmpshortlangcode = '';
|
||||
if ($obj->lang) $tmpshortlangcode = preg_replace('/[_-].*$/', '', $obj->lang); // en_US or en-US -> en
|
||||
if ($tmpshortlangcode != $shortlangcode) {
|
||||
$tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $obj->id ? '/' : '/'.$obj->pageurl.'.php').'" />'."\n";
|
||||
$tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $obj->id ? '/' : '/'.$tmpshortlangcode.'/'.$obj->pageurl.'.php').'" />'."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,11 +183,11 @@ function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage)
|
||||
else dol_print_error($db);
|
||||
}
|
||||
// Add canonical reference
|
||||
$tplcontent .= '<link href="/'.(($objectpage->id == $object->fk_default_home) ? '' : ($objectpage->pageurl.'.php')).'" rel="canonical" />'."\n";
|
||||
$tplcontent .= '<link href="/'.(($objectpage->id == $object->fk_default_home) ? '' : $shortlangcode.'/'.($objectpage->pageurl.'.php')).'" rel="canonical" />'."\n";
|
||||
// Add manifest.json on homepage
|
||||
$tplcontent .= '<?php if ($website->use_manifest) { print \'<link rel="manifest" href="/manifest.json.php" />\'."\n"; } ?>'."\n";
|
||||
$tplcontent .= '<!-- Include link to CSS file -->'."\n";
|
||||
$tplcontent .= '<link rel="stylesheet" href="styles.css.php?website=<?php echo $websitekey; ?>" type="text/css" />'."\n";
|
||||
$tplcontent .= '<link rel="stylesheet" href="/styles.css.php?website=<?php echo $websitekey; ?>" type="text/css" />'."\n";
|
||||
$tplcontent .= '<!-- Include HTML header from common file -->'."\n";
|
||||
$tplcontent .= '<?php print preg_replace(\'/<\/?html>/ims\', \'\', file_get_contents(DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html")); ?>'."\n";
|
||||
$tplcontent .= '<!-- Include HTML header from page header block -->'."\n";
|
||||
|
||||
Reference in New Issue
Block a user