Solprovider Lenya Kongregate Registration for Free Flash Games and Chat with solprovider

Initial Language for Visitors

The homepage defaults to the default language of a publication. The following is about changing the homepage to use the browser's language. Any multi-language website should provide the ability to change languages. This code is a hack because the website I am building has only 2 languages; later are suggestions for creating a more generic solution, but Cocoon does not make it easy.

To control the initial language
FILE: publication-sitemap.xmap
After:
<map:match pattern="**/">
<map:redirect-to uri="index.html"/>
</map:match>

Add:
<map:match pattern="**/index.html">
<map:select type="parameter">
<map:parameter name="parameter-selector-test"
value="{request:locale}"/>
<map:when test="xx">
<map:redirect-to uri="index_xx.html"/>
</map:when>
<map:when test="xx_XX">
<map:redirect-to uri="index_xx.html"/>
</map:when>
<map:otherwise>
<map:redirect-to uri="index_en.html"/>
</map:otherwise>
</map:select>
</map:match>

Repeat the map:when clauses for each locale. If some locales include the country, you would need to specify each separately using the second example. Replace the final "index_en.html" with your default language.

A (better) universal solution would extract the language code from request:locale, then resource-exists check the correct index_xx.xml, and either redirect to it (if it exists), or use the default language (if not). The code would look like this:
THIS CODE DOES NOT WORK!

<map:match pattern="**/index.html">
<map:select type="resource-exists">
<map:when test="{1}/index/index_" +
substring({request:locale}, 1, 2) + ".xml">
<map:redirect-to uri="index_" +
substring({request:locale}, 1, 2) + ".html"/>
</map:when>
</map:select>
<map:redirect-to uri="index_en.html"/>
</map:match>

I could not discover how to call substring() within an XMAP. A workaround would be to send it to a JavaScript function, but that is too complex for such simple functionality. Anybody else know?

<< ProblemsVisitors' Choice >>

Contact Solprovider
Paul Ercolino