Preamble

We wanted a way of working with Locales, so that a user of our sites could see dates etc. formatted automatically to their Locale, or could override their browser's locale with a specific one.

I will primarily be looking at Locale usage in Woody and JXTemplate.

The other issue we had was that we did not want to use the LocaleAction, Actions are going out of fashion ....

Getting and Setting the Locale in your Sitemap:

You will need access to the User's Locale in the Sitemap, so that you can pass it to the Sitemap components that use it, like the Woody and i18n Transformers.

This has traditionally been done with the LocaleAction, but we chose to do this instead with InputModules:

We set up this group of InputModules in our cocoon.xconf:

{{{<component-instance name="myapp-defaults"

</component-instance>

<component-instance name="locale"

</component-instance> }}}

I have set up two InputModules, myapp-defaults contains default values for our application, one of which is a fall-back Locale. In the case of Locale, I am not sure if this is absolutely necessary, but we have other application defaults, so I thought I might as well stick this in.

The second InputModule, locale uses the ChainMetaModule, it allows you to setup a list of modules that will be asked in turn for a value, until one of them returns one.

In this case, we first look in a possible Request Parameter, so that ?locale=fr_fr could be added to the end of URLs to override built-in locales. If that Parameter does not exist, then next we look in the Request Locale Object, and just in-case there is not one (is this ever the case?) we look up the default.

Sample usage in the !SiteMap:

Woody and i18n Transformers are set up in the normal way:

{{{<map:transformers default="xslt">

</map:transformers>

}}}

First a typical Woody Screen pipeline: {{{<map:match pattern="screen/woody/*">

</map:match> }}} Note we are giving both WoodyTransformer and i18nTransformer the locale.

Here is a sample JXTemplate pipeline: {{{<map:match pattern="screen/jx/*">

</map:match>

}}}

i18n in action

In these examples I will look at date formatting .... in our project, we are using JXTemplate to view database records and Woody to edit them (though in this case we are not editing dates, just making sure they are displayed consistently in the User's locale).

JXTemplate Sample

First I show how to format dates in JXTemplate by using i18n tags: {{{<document xmlns:t="http://apache.org/cocoon/templates/jx/1.0"

</document> }}} First we have to setup the required namespaces for JXTemplate and i18n. Next, assuming our date is in the modified parameter of our model, we format that date for the User's Locale. We provide the src-format telling i18n which format the supplied date is in and the pattern to output it in. In this sample we use one of the format shortcuts short please see the i18nTransformer documentation to see what other option you have.

Woody Sample

Next is a sample from Woody, this is from one of our Woody Model files: {{{<wd:form

</wd:form> }}} Again, we first setup namespaces for Woody and i18n. Next, assuming we have a modified property on our Form, we make a wd:output for that property (in our Application the dates are not editable). The woody output widget uses an i18n tag for the wd:label, this gets looked up in our local catalogue. And a datetime formatting converter for the date itself. Note we are able to use the same short formatting shortcut. Please see the Woody documentation to see what other options you have.

The outcome

When Users browse our site, they now always see dates formatted according to their local custom (here I show full dates, so the difference is more clear).

So far so good .....

You also have the option of letting users view your site in a specific Locale, as mentioned above, by adding the locale Request Parameter.

These techniques will all obviously work with other aspects of i18n, like number and currency formatting, text-translations etc.

References

WorkingWithLocales (last edited 2009-09-20 23:41:49 by localhost)