|
Cookbook /
MultiLanguageViewsSummary: show language specific content and titles according to user choice
Version: 2010-07-05
Prerequisites: PmWiki 2.2
Status: new
Maintainer: HansB
Download: langviews.phpΔ
Questions answered by this recipeHow can I show language specific content and titles according to user choice? DescriptionThis recipe adds the following tools to help implement multiple language view features:
These tools can be used to add language switching features, which will give a user the choice to select from various languages, and the editors and admin to include language specific content and titles in a page. InstallationInstall langviews.phpΔ to the cookbook folder and add to config.php: include_once("$FarmD/cookbook/langviews.php");
The configuration variables ( Optionally download flags.zipΔ to add some flag images for use with language selecting links. Details on usage:config settings:$MLVList default array for language id's and labels: $MLVList = array(
'en' => 'english',
'de' => 'deutsch',
'fr' => 'francais',
'it' => 'italiano',
'es' => 'espanol',
);
Define your own version of $MLVList in config.php before including the recipe script. Use ISO 639-1 2 letter language codes. $MLVDefault = 'en'; default language id. Set to different default in config.php before including langviews.php. $MLVSelectDefault= 'id'; default link type for (:selectlang:) markup, set in config.php to different link type before including langviews.php ('label' or 'flag'). $MLVLinkPatterns default array, for defining (:selectlang:) link formats. Note that '$page', '$id' and '$label' are used as pseudo-variables: SDVA($MLVLinkPatterns, array( // links with id label 'id' => '[[$page?setlang=$id|$id]]', // links with text label 'label' => '[[$page?setlang=$id|$label]]', // links with images (flags) plus tooltip label 'flag' => '[[$page?setlang=$id|Attach:Site/Site/$id.png"$label"]]', )); $EnableMLVBackwardsCompatibility = 0; : set to 'true' in config to enable backward compatibility settings for sites migrating from multilanguage.php. By default the compatibility features (extra variable, page variable, conditionals etc.) are disabled. See Compatibility below. $MLVCookie: by default the cookie name is 'userlang', with a $CookiePrefix if set (for wiki-specific cookie setting). $EnableMLVSwitching = 1;: set to false in config to disable language switching via cookie setting. To change the wiki interface language according to the language view, add something like this to config.php, after including the langviews.php script: if ($_COOKIE[$MLVCookie] == 'de') {
XLPage('de','PmWikiDe.XLPage');
XLPage('de','PmWikiDe.XLPageCookbook');
}
etc. for other language's XL pages url (and link) parameters:?setlang=<id> url parameter for switching language by setting a cookie: create action links for switching the language, like ?lang=<id> url parameter for non-permanent (non-cookie) language switching. ?userlang=<id> url parameter is retained as part of backwards-compatibility with sites migrating from multilanguage.php; you need to set $EnableMLVBackwardsCompatibility = 1;. See Compatibility below. markup directives:
(:pagelist group=SomeGroup if="langinpage {$Lang} {=$FullName}" fmt=title:)
page variables:{$Lang} page variable: Display the current language id in the page or use it in pagelist markup etc. {$LangLabel} page variable: Display the current language label text. {$lang} and {$langlabel} (with all letters low case) work as well. {$userlang} page variable is retained for backwards-compatibility with sites migrating from multilanguage.php; you need to set $EnableMLVBackwardsCompatibility = 1;. It is the same as {$Lang}. See Compatibility below. Compatibility with existing MultiLanguage markup in pagesBy setting $EnableMLVBackwardsCompatibility = 1; the following variables and markup is added:
Also note:
Switching from MultiLanguage to this recipe should cause little problems, but you cannot include both recipes at the same time (nor should there be any need). You will need to set specific config variables, which are different from multilanguage.php. NotesTo use
## Setting the language cookie equals modifying the wiki, thus we invalidate cache
if ($EnableIMSCaching && isset($_GET['setlang'])) {
SDV($LastModFile,"$WorkDir/.lastmod");
@touch($LastModFile);
}
To Hans: even though it would be useful to add this to the recipe, the only bulletproof way to do this would be to explicitly state that Release notes
See alsoContributorsCommentsDinalogic 2010-07-18: I made a little modification (only 3 lines of code) for basic styling of language selector, so you can introduce a custom separator between consecutive labels. On the Inside the loop, modify the last line: and add a new line: With this, you can use the "sep" variable when putting the language selector in your template for defining a html separator. For instance, with (:selectlang lang=en,es sep= | :) you'll get something like Antony Templier 2010-09-23:I found a bug. If you use (:selectlang flag:) used in Site/SideBar page, and you choose a page on the wiki in which there is no translation ( i mean no tag (:if lang <id>:) ), then this Attach:Site/Site/.png"" is displayed in the menu. Workaround : To be sure nothing appends when there is no translation, I add a extra condition in the function MLVSelectLangMarkup just before the loop foreach ($plang as $k) at line 171 of the langview.php file.
if(count($plang)>1){
foreach ($plang as $k) {
$str = '';
$str = str_replace('$id', $k, $MLVLinkPatterns[$type]);
$str = str_replace('$label', $MLVList[$k], $str);
$out .= str_replace('$page', $pagename, $str).' ';
}
}
Damien November 23, 2010, at 02:40 PM Thanks, I am happy yo use this recipe. But it is unclear where I should place the flag images, as this syntax does not look as standard. I can probably replace it with Site./$id.png, but then it is more a problem to install an update. Damien November 23, 2010, at 02:48 PM Upon testing I see that both syntaxes are equivalent Site./$id.png and Site/Site/$id.png Sorse - 2011-09-20: I found a bug. When using multiple wiki interface languages, defined with if ($_COOKIE[$MLVCookie] == 'de') {
XLPage('de','PmWikiDe.XLPage');
XLPage('de','PmWikiDe.XLPageCookbook');
}
etc. for other language's XL pages the language of header/footer changes only after a second click on the language switch. It has to do with the way changes in cookies are reflected. The solution is similar to the one given for the MultiLanguage recipe: edit langviews.php around line 74 if (@$MLVList[$sl]) {
if (!isset($_GET['lang'])) setcookie($MLVCookie,$sl,$MLVCookieExpires,'/');
$Lang = $sl; unset($sl);
} else $Lang = $MLVDefault;
$_COOKIE[$MLVCookie]=$Lang; # add this line
}; Language specific skins picturesTo make some pictures of a skin language specific simply
User notes +3: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki. |