Localization
- Multi-language site structure
- Switch between languages
- Automatic detection of user's preferred language
- Useing the Administration View tools for multi-language site creation
Contents |
Multi-language site structure
To achieve a multi-language site in opencms it's a good idea to create one top-level folder per language. These top-level folders are usually named after their two-letter lowercase code as identified by the ISO 639-1 list, although you can name them however you want. On each of these folders, the locale property should be defined. This can be done by left-clicking the folder icon and selecting properties > advanced. In the advanced properties the locale property should be listed. All the child folders and pages will then inherit the locale of their parent folder. If you would like a page to be available in several locales, you start out by creating the page in one of the locale folders, after which you create a sibling to this page in the folders of the other locales for which you want this page to be available.
Caveats
This section will contain a number of pitfalls related to developing or working with a multilingual site in OpenCms.
NullPointerException when saving a Page in a non-default locale
When your site needs to support several languages, and you are using the "default body" as your "Copy body from" option, you might run into NullPointerExceptions when trying to save a Page in a locale different from the locale in which the Page was created.
This happens because the default template does not provide a <body> element for all languages.
Example body supporting multiple languages
An example body that has the same *body element as the "default body" provided by OpenCms but supports multiple languages would look as follows:
<?xml version="1.0" encoding="UTF-8"?> <pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.opencms.org/dtd/6.0/xmlpage.xsd"> <page language="nl"> <element name="body"> <links/> <content><![CDATA[]]></content> </element> </page> <page language="fr"> <element name="body"> <links/> <content><![CDATA[]]></content> </element> </page> <page language="en"> <element name="body"> <links/> <content><![CDATA[]]></content> </element> </page> <page language="de"> <element name="body"> <links/> <content><![CDATA[]]></content> </element> </page> </pages>
This example would create a body element for the dutch, english, french and german versions of the Page. You can either edit the existing "default body" provided by OpenCms, or create your own body if you are working on a custom module.
Editing the default body
The "default body" file can be found in the folder /system/modules/org.opencms.welcome/default_bodies/
Creating a custom body
If you have created your own module, you can add custom bodies by creating the default_bodies folder in /system/modules/your.module.name/
Is this a bug
It looks like this is a bug with the way OpenCms tries to save Pages of the type "Page with free text". If a particular element is missing for the selected locale, it should be created on saving. Additionally, if one or more extra language(s) are added to a site in the future, the Control Code of all the existing "Page with free text" files will have to be manually updated to support the new language(s).XMLContent does not suffer from this problem.