RE: [xsl] Parameters in localized messages

Subject: RE: [xsl] Parameters in localized messages
From: "Jeroen Hellingman" <jeroen@xxxxxxxx>
Date: Tue, 20 Apr 2004 08:36:31 -0400
> msgContact is an XPath expression - it means child::msgContact, and 
selects
> a child element of the context node. I think you wanted to pass a 
string:
> select="'msgContact'".

You're right, my mistakes in quickly typing up the general idea of the 
problem I am facing:

I would like to have some generic localization mechanism, in which I 
can load a localization database using something like document
('messages.xml'), and have these messages have named parameters, that 
is, the messages.xml file would contain lines like:

<message id="msgContact" lang="en">Please contact <param 
name="personalName"/> 
for details</message>
<message id="msgContact" lang="nl">Neem contact op met <param 
name="personalName"/> voor details</message>

Where the number of <param/> tags and names of these tags are unknown 
beforehand.

I would then load the content of my message.xml into a variable, 
$messages, and do something like <xsl:value-of select="$messages/message
[@id='msgContent']/>. If the message where a flat message, that would 
be enough, but I still want to render the <param/> tags in this 
localized string.

In a more traditional code, we would replace {%name} with an actual 
value in the following string: "Please contact {%name} for details"

As an alternative, I could pre-transform my messages database to an xsl 
stylesheet, and generate something like

<xsl:template name="msgContact">
   <xsl:param name="personalName" value="Somebody">
      Please contact <xsl:value-of select="$personalName"/> for details
</xsl:template>

However, this would still lead me to trouble, as I can only determine 
the language to use when the document is being processed. In the xslt 
cookbook, a technique, using 

<xlst:apply-templates select("document('')/*/f:func[@name=$aTemp1]"/> 

is used, which I could apply here:

<f:funct name="msgContact" lang="en"/>
<xsl:template name="msgContact">
   <xsl:param name="personalName" value="Somebody">
      Please contact <xsl:value-of select="$personalName"/> for details
</xsl:template>

<f:funct name="msgContact" lang="nl"/>
<xsl:template name="msgContact">
   <xsl:param name="personalName" value="Somebody">
      Neem kontact op met <xsl:value-of select="$personalName"/> voor 
details
</xsl:template>

And call them with something like

<xsl:apply-templates select="document('')/*/f:funct[@name='msgContact' 
&& @lang=$lang]">
   <with-param name="personalName" value="$Somebody"/>
</xsl:apply-templates>


However, this still requires me to compile my message database.

Is there are more elegant or direct way to achieve this?



-- 
Jeroen Hellingman
Aletta Jacobsstraat 5
3404 XD IJsselstein
+31 30 6875444
http://www.bohol.ph

Current Thread