2013-07-23, 23:53:37
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');
insert after the i18n_init() function
in plugins/i18n_base/frontend.class.php
insert before the load($lang) function
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
and after
if ($menu) self::$pages[$url]['menu_'.$lang] = stripslashes($menu);
if ($title) self::$pages[$url]['title_'.$lang] = stripslashes($title);
the following
in public static function getMenu
add after
$slug = '' . $slug;
the following
in private static function getMenuImpl
add after
'title' => self::getProperty($childurl,'title',$deflang),
'link' => self::getProperty($childurl,'link',$deflang),
the following
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
And for the administration
in plugins/i18n_base/pages.php
change
$lang = substr($data->url,$pos+1);
to
add after (before the closing "}" )
if (isset($page['variants'][$lang])) {
$variant =& $page['variants'][$lang];
if ($variant['title'] == '') $variant['title'] = '[No Title] » <em>'. $variant['url'] .'</em>';
the following
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
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)
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($slug, 0, $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] » <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)