PmWiki | Cookbook / MultiLanguage (original) (raw)

Summary: Display content in different languages on a page by user's choice

Version: 20230409

Prerequisites: pmwiki 2.2.62

Status:

Question

How can I publish text of a wiki-page in different languages depending on user's choice?

Answer

Features

Notes

Installation:

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

See Also

You can see the recipe live here (note the links on top of the left bar).

Contributors

Comments

See discussion at MultiLanguage-Talk

User notes : 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.