Thread Rating:
  • 3 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Support for multilanguage sites, Internationalization (I18N)
Hello,

first of all I'd like to thank mvlcek for his excellent plugins but I always missed one feature, the possibility for having a localized slug. So I tried to write it myself and here's my working solution.

It simply works with adding _yourLocalSlug at the end of your slug

An example for a "About" page in english (default), french and german

english slug = about
french slug = about_fr_a-propos (frontend page url = fr/a-propos)
german slug = about_de_ueber-uns (frontend page url = de/ueber-uns)

but you don't have to add the translated slug so it won't break your existing pages, you can still use
french slug = about_fr (frontend page url = fr/about)


For this to work you have to make the following changes in your php files

in plugins/i18n_base.php

insert before add_action('index-pretemplate', 'i18n_init');

PHP Code:
add_action('error-404''i18n_local_slug'); 

insert after the i18n_init() function

PHP Code:
function i18n_local_slug() {
  global 
$id;
  require_once(
GSPLUGINPATH.'i18n_base/frontend.class.php');
  
I18nFrontend::setLocalizedSlug($id);


in plugins/i18n_base/frontend.class.php

insert before the load($lang) function

PHP Code:
public static function setLocalizedSlug($id) {
      global 
$data_index;
    
    
$langcode substr(self::getLangURL(), -3, -1);
    
    
$file_array glob(GSDATAPAGESPATH '*' '_' $langcode '_' $id .'.xml');
    
    if(
file_exists($file_array[0])) {
        
$data_index getXml($file_array[0]);
    }
  } 

The code can normally be inserted everywhere in these files (maybe not the add_action, but not sure) but it seemed to me to be appropriate like this.

For the navigation

in plugins/i18n_navigation/frontend.class.php

in public static function getPages

add after
if (strpos($filename,'_') !== false) {
$pos = strpos($data->url,'_');
$url = substr($data->url,0,$pos);
$lang = substr($data->url,$pos+1);

the following
PHP Code:
if (strlen($lang) > 2) {
    
$pos strpos($lang,'_');
    
$slug substr($lang,$pos+1);
    
$lang substr($lang,0,$pos);
    } 

and after
if ($menu) self::$pages[$url]['menu_'.$lang] = stripslashes($menu);
if ($title) self::$pages[$url]['title_'.$lang] = stripslashes($title);

the following
PHP Code:
if (isset($slug)) self::$pages[$url]['slug_'.$lang] = $slug

in public static function getMenu
add after
$slug = '' . $slug;

the following
PHP Code:
$pos strpos($slug'_');
if(
$pos 0$slug substr($slug0$pos); 

in private static function getMenuImpl
add after
'title' => self::getProperty($childurl,'title',$deflang),
'link' => self::getProperty($childurl,'link',$deflang),

the following
PHP Code:
'slug' => self::getProperty($childurl,'slug',$deflang), 

in public static function getMenuHTMLImpl

replace
$href = @$item['link'] ? $item['link'] : (function_exists('find_i18n_url') ? find_i18n_url($item['url'],$item['parent']) : find_url($item['url'],$item['parent']));

with
PHP Code:
if (!isset($item['slug'])) {
$href = @$item['link'] ? $item['link'] : (function_exists('find_i18n_url') ? find_i18n_url($item['url'],$item['parent']) : find_url($item['url'],$item['parent']));
}
else {
$href = @$item['link'] ? $item['link'] : (function_exists('find_i18n_url') ? find_i18n_url($item['slug'],$item['parent']) : find_url($item['slug'],$item['parent']));


And for the administration

in plugins/i18n_base/pages.php

change
$lang = substr($data->url,$pos+1);
to
PHP Code:
$lang substr($data->url,$pos+1,2); 

add after (before the closing "}" )
if (isset($page['variants'][$lang])) {
$variant =& $page['variants'][$lang];
if ($variant['title'] == '') $variant['title'] = '[No Title] &nbsp;&raquo;&nbsp; <em>'. $variant['url'] .'</em>';

the following
PHP Code:
$localslug $variant['url'];
$localslugarray explode("_"$localslug);

if (isset(
$localslugarray[2])) {
    
$localslug $localslugarray[2];
    }
else {
    
$localslug $localslugarray[0];
    }
    
$file_array glob(GSDATAPAGESPATH $page['parent'] . '_' $lang '_' '*' .'.xml');
    
if(
file_exists($file_array[0])) {
$localslugparentarray explode("_"basename($file_array[0], ".xml"));

if (isset(
$localslugparentarray[2])) {
    
$localslugparent $localslugparentarray[2];
    }
else {
    
$localslugparent $page['parent'];
    } 

and replace
<a title="<?php echo i18n_r('VIEWPAGE_TITLE').': '.stripslashes($variant['title']); ?>" target="_blank" href="<?php echo find_i18n_url($page['url'],$page['parent'],$lang); ?>">#</a>

with
Code:
<a title="<?php echo i18n_r('VIEWPAGE_TITLE').': '.stripslashes($variant['title']); ?>" target="_blank" href="<?php echo find_i18n_url($localslug,$localslugparent,$lang); ?>">#</a>

That's it (if I didn't forget to write down some changes I made).

I tested it with a newly installed GetSimple 3.2.1 and I18N 3.2.2 and fixed every issue I encountered.

This can probably be made in a more elegant way and I hope mvlcek will add this to his plugin (and fix the compatibility issues that will probably come with the other I18N plugins)
Reply


Messages In This Thread
rewrite language problem - by tomkop - 2013-03-27, 06:34:44
Enhancement suggestion - by hameau - 2013-05-12, 04:17:12
Enhancement request - by hameau - 2013-06-14, 20:02:07
RE: Enhancement request - by mvlcek - 2013-06-14, 22:04:12
RE: Enhancement request - by hameau - 2013-06-14, 23:01:36
RE: Support for multilanguage sites, Internationalization (I18N) - by bob_lennon - 2013-07-23, 23:53:37
Duplicate language cookies bug - by enzy - 2013-08-20, 07:03:26
RE: Duplicate language cookies bug - by ohayo - 2013-08-20, 18:12:25
Plugin is not shown - by tosho - 2013-09-15, 12:10:37
1000 pages limit in i18n_navigation - by titus - 2014-10-13, 21:58:36
Bug found - by lbarsov - 2014-10-17, 03:41:53
Menu with picture - by Dimi - 2015-04-28, 16:05:45
Rendering Menu in Component - by Alexander_ - 2016-05-04, 23:06:51
Meta desc for different languages - by Alexander_ - 2017-02-14, 20:29:32
Hierarchical Menus - by celextel - 2019-07-26, 16:41:47
Ri18n_navigation - by celextel - 2021-02-19, 16:07:48



Users browsing this thread: 9 Guest(s)