TIP #399 Version 1.3: Dynamic Locale Changing for msgcat

This is not necessarily the current version of this TIP.


TIP:399
Title:Dynamic Locale Changing for msgcat
Version:$Revision: 1.3 $
Authors: Harald Oehlmann <Harald dot Oehlmann at elmicron dot de>
Harald Oehlmann <harald dot oehlmann at elmicron dot de>
State:Draft
Type:Project
Tcl-Version:8.6
Vote:Pending
Created:Tuesday, 27 March 2012
Keywords:Tcl, localization

Abstract

This TIP adds dynamic locale switching to the msgcat package.

Abstract

This TIP adds dynamic locale switching to the msgcat package.

Rationale

The msgcat package has a 3 stage processing model:

1. Set locale list

mclocale locale

2. Load language files with other package load

mcload catalog

3. Translate strings

mc key args...

If the locale should be changed after other packages are loaded, one must restart at step 2. This implies to reload all packages which is mostly not passible.

Within a multi-language application like a web-server, one may change the language quite quickly, for example if users with different locales are requesting pages.

The issue is that mcload only loads language files included in the current locale (mcpreferences) and does not load any others.

The aim of this tip is to extend mcload to load additional language files. Then mclocale may be called to change the language on runtime.

Specification

This TIP proposes to add a new command:

msgcat::mcconfig -pattern ?patternlist?

This command may get or set package options. There is currently one option "-pattern". Options may be set using

msgcat::mcconfig option value ?option? ?value?

Current option values may be read using:

msgcat::mcconfig option

The option '-pattern consists of a list of language file name patterns like fr*, *, fr_ch.

Example Usage

Imagine an application which supports the current user language and french, german and english.

An external package tp is used. The package uses msgcat and performes within the package require tp call:

package require msgcat
msgcat::mcload [file join [file dirname [info script]] msgs]

An implementation of the application with the current msgcat 1.4.4 would require the following initialisation sequence:

package require msgcat
package require np

and the following code to change the language to french:

package forget np
msgcat::mclocale fr
package require np

Using the extension of this tip, the required code for initialisation is:

package require msgcat
msgcat::mcconfig -pattern {fr* de* en*}
package require np

and to change to french language:

msgcat::mclocale fr

Within this modification, language change is a cheap operation. It was computational expensive (if not impossible)

Reference Implementation

See Tcl Feature Request 3511941. [1]

Compatibility

No incompatibilities are introduced. But packages might not be aware of a language change and may buffer translations outside of msgcat. There is no way to fix this but modifying the package.

Alternatives

This implementation requires the setting of the pattern before any package with msgcat is loaded. To avoid this, msgcat must store all paths passed by any mcload call. In case of a locale change, any currently missing files are loaded. This requires much more housekeeping and may lead to side effects, especially if packages are not aware of the fact that their package files are loaded outside of the mcload command.

Copyright

This document has been placed in the public domain.


Powered by TclThis is not necessarily the current version of this TIP.

TIP AutoGenerator - written by Donal K. Fellows