|
Cookbook /
MultiLanguageSummary: Display content in different languages on a a page by user's choice
Version: 2007-07-05
Prerequisites: pmwiki 2.1
Status:
Maintainer: Blues
QuestionHow can I publish text of a wiki-page in different languages depending on user's choice?
AnswerYou can use multilanguage.phpΔ.
Features
NotesInstallation:Download the script from above and place it in your cookbook directory. Then add include_once('cookbook/multilanguage.php'); in your local configuration (local/config.php).
Configuration:Depending on which languages you will use in your wiki pages you will want to use the UTF8 features of PmWiki by adding include_once('scripts/xlpage-utf-8.php'); to your config.php before including the multilanguage.php.
Before including the multilanguage.php you may give a value to the following variables:
$DefaultLanguages is an array of preferred languages, it is used to display a default language if the selected one is not available in the page. The default is $DefaultLanguages = array('en');
ex. $DefaultLanguages = array('en','it','de');
If the user selected 'es' and in the current page only 'it' and 'de' are defined, then 'it' will be displayed.
$LanguageSelectionFmt can be used to personalize the output of the (:selectlang:) directive. Default is $LanguageSelectionFmt = '[[{$FullName}?userlang=$1|$1]] '; which will display links in the form: "en it de".
ex. $LanguageSelectionFmt = '[[{$FullName}?userlang=$1|Attach:$1.gif]] '; will display links with flags (provided you upload the flags images).
If you use translation pages (see: Internationalizations) you probably want to switch the XLPage definition too. To do this, use an if statement in your local configuration (config.php):
if ($_COOKIE['userlang'] == 'XX') {
XLPage('XX','PmWikiXX.XLPageCookbook');
XLPage('XX','PmWikiXX.XLPage');
XLPage('XX','PmWikiXX.XLPageLocal');
}
Use PmWikiXX.XLPageLocal for your own phrases
Use:The page content with multilanguage support can be defined including each language in a (:if userlang xx:) section.
ex.
(:if userlang en:)
english text
(:if userlang it:)
italian text
...
(:if:)
common text
The page variable {$userlang} holds the currently selected language (eg. "en").
You can put (:selectlang:) anywere in a page to display links to the available languages for that page.
The following parameters can be used:
page=... to specify the page for which display the links.
default=... to specify a default page for languages present in the $DefaultLanguages but who are not defined in the page (useful to direct the user to a homepage).
A new langinpage conditional is available, to test whether in a particular page a particular language is defined or not. The conditional takes two parameters, the language and the pagename.
eg.
(:if langinpage en MyGroup.MyPage:)
tests if in the page MyGroup.MyPage is defined the language "en".
eg.
(:pagelist if="langinpage {$userlang} {=$FullName}":)
lists all the pages in which is defined the language currently selected by the user.
Notes:Available languages and multilanguage page titles are parsed on edit and saved as page attributes to speed-up parsing on page load.
Examples:You can see the recipe live here (note the links on top of the left bar).
Releases
CommentsAnyone a idea how to add a active-language page variable to this script? The following seams not to work: Oky I got one, don't no if it's best practice: $userlang = $_COOKIE['userlang'];
noskule July 02, 2007, at 06:59 AM I can't get the DefaultLanguage to show. Language pages are only shown after a cookie was set. The text after the (:if:) tag is shown to in all languages. Debugging a bit: it has something to do with $PCache[$pagename]['languages']. It is not loading the languages key from the wiki file. I couldn't find the way to load those keys, here on pmwiki.org. Renato Very nice script. I'm just wondering how I change XLPage definitions. I tried with
if ($userlang == 'de') {
XLPage('de','Site.NetstreamsTranslation');
XLPage('de','PmWikiDe.XLPageCookbook');
XLPage('de','PmWikiDe.XLPage');
}
This seams only to work if userlang is spezified in the url with ?userlang=XX, or if there is a (if userlang XX) statement is in the page. How do I change this that it keeps the language also if there is no (:if userlang . .. staement?noskule April 15, 2006, at 03:58 PM Yes, I have the same problem (lean skin). flox April 15, 2006, at 05:30 PM
I guess this is because the script only gests executed if a (:if userlang XX markup is present in the wikitext. So we have to search a way to execute the script with every pageload not only if the markup is present. Do you have any idea how? Better would be a variable $ExecuteMultilanguageOnPageLoad = 1 (mabe with a shorter name) . . ..noskule April 16, 2006, at 04:32 AM
The script is already executed at every page load even if no multilanguage markup is present. The problem is in the way that default languages work. For example, if the user selected german (with a ?userlang=de in the URL but in the current page german is not defined, then the language will automatically switch to the default (let's say english), thus $userlang becomes 'en'.The original language request of the user, though, is kept in a cookie so that if the user which selected german will at a certain point go to a page where german is defined then it will display german. So, if you wish to select the XL page regardless of the page definition but according to user selection, you can use the cookie ( $_COOKIE['userlang']).eg. if ($_COOKIE['userlang'] == 'de') { ... I hope that it is enough for your needs. Blues April 17, 2006, at 03:11 AM
Blues April 17, 2006, at 06:27 AM I have a problem: my Site.SideBar contains English and Italian versions, however only some of my main pages contain Italian versions. On a page with no Italian version the English version of the SideBar is used instead of the Italian version. Is there a way to fix this? Also, if the page doesn't contain any Chris Morison? August 08, 2006, at 05:39 AM I have the same problem as above. I love this recipe but I would like to have more flexibility. Let me try to describe my view about this. Philippe? August 19, 2006, at 05:39 PM I'm also wondering about the above sidebar problem. I really don't want to have to go through all my files and add Kale Stutzman? September 11, 2006, at 10:19 AM My solution is as follows. Edit the multilanguage.php script and insert the lines:
$userlang2=$userlang;
$Conditions['userlang2'] = '$GLOBALS[\'userlang2\']==$condparm';
before line 54:
if ($pagename=='') {
Then only use (:if userlang ..:) on main pages, and use (:if userlang2 ..:) on SideBar, Footer, and any other pages. The problem is caused by the variable userlang being reset to default if the current main page doesn't contain the chosen language, forcing the SideBar and other pages into a different language. My solution works by setting userlang2 before userlang is reset. Works for me, but please test first...
Chris Morison? September 21, 2006, at 10:07 AM It does work for me as well, Chris. Just, if you ask for a language which is not defined with (:if userlang2 ..:) then the SideBar comes out empty, again.
Luigi November 27, 2006 I can't find out how to specify a different page title for each language ("multilanguage titles support"). Might this be a misunterstanding, is there no possibility to translate the page titles? Martin Zbinden, November 26, 2006 Please try: (:if userlang tr:) (:title Turkish Title:) Turkish text (:if userlang de:) (:title German Title:) German text (:if userlang it:) (:title Italian Title:) Italian text (:if:) Luigi November 27, 2006 Very useful script. I have two problems though. First, when PageNotFound is loaded, the language links become edit links ?action=edit and are lacking ?userlang=XX. This is a problem because the page creation reverts to the default language instead of the current userlang. Second, I have to click on the language switch link twice before it "takes" on headers/footers. I can see that the cookie does change to the new language - and the page text too - on the first click, but the headers/footers don't. SteP 7 February, 2007 I found how to fix both problems. I think this fix may fix other people's problems, and make userlang2, as proposed in another comment, unnecessary. Change the code to set the cookie array userlang element for the current page -- see php manual, setcookie common errors. So in version 1.0b of the script, tested with PmWiki 2.1.27, change around line 52 } else {
$userlang = $DefaultLanguages[0];
setcookie('userlang',$userlang,0,'/');
}
$_COOKIE['userlang']=$userlang; // add this line
It fixed all my problems. Good luck. SteP February 12, 2007, at 03:35 PM Somebody know if is possible that with this recipe the links generated to another language goes to a different site that also is using PMWiki. Example: I have http://www.reinashinohara.com/index.php?n=PmWiki.Hiragana I want to put the english option on the left for this page but going not in the same domain.. if somebody clicks in english then will be sent to http://learnjapanesepod.com/wiki/index.php?n=Main.Hiragana. Is this possible? Thanks in advance Pablo Version 1.1 works better with the Category group than the previous versions, however there's still a problem. In Category.GroupFooter included by Category pages, userlang is set to the default language, instead of the language set in the cookie. This ends up displaying the skin in the cookie language, and page contents in the default language.
I tried this fix which seems to be working OK here, though it needs more testing on sites where multilanguage.php is in use.
85 } else {
86 $userlang = $_COOKIE['userlang']; // SteP added
87 if($userlang=='') // SteP added
88 $userlang = $DefaultLanguages[0];
Good luck! SteP August 19, 2007, at 08:53 PM I had this problem not with GroupFooter(s), but with the whole site. If your wiki is only displaying the default language translation, use this fix. Renato There's a problem when used with the IncludePage function: if there is e.g. a specification to include a specific number of lines into the current page, enclosed in multiple if statements, this works correctly in the first language, but in the second one it hides the entire section, where the page was supposed to be placed. JanBrejcha Caution: # 1.0b - 03.04.2006 fix (Fixed a problem when $pagename is not set) Buz if you use famconfig.php mulitlangange.php must be after execution of config.php.
But I'm wondering, wether this fix is correct all, as it's IMHO the task of pmwiki to assemble the pages. My application find's the correct entries again when commenting out this fix. See Also
You can see the recipe live here (note the links on top of the left bar).
ContributorsOld commentsDefault language doesn't work. So if i have two languages in a page, 'de' and 'en', no one is displayed. But I set $DefaultLanguages = array('de','en'); flox (19.03.2006) Did you set the $DefaultLanguages before including the script? If yes, could you explain better? Make some experiment with different combinations?
Blues March 30, 2006, at 06:32 AM Yes. Before including the script. You can test it here. I have to use for example?userlang=en, before the variable userlang is avaiable. flox March 31, 2006, at 02:41 PM Please try the new version (1.0b) and see if the problem is solved.
Blues April 03, 2006, at 05:38 AM
flox |