Mutli-lingual Page Layout Text for Site Variations Part 2 - Using Resource Files
OK, so in part 1 we used javascript to update headings in a page layout, depending on what variation we were in. That might be fine for some situations, but it has major shortcomings, not least of which guaranteeing that your clients have javascript turned on. A slightly more fiddly but much more satisfactory solution dispenses with script altogether. Put the headings into custom Resource Files!
So at the risk of repeating part 1, we start with is a page that looks like this;
For my French Variation, I need the above to look like this;
Do you remember all that horrid javascript from Part 1? Imagine just ditching it all, and where you need your header text simply putting this;
<asp:literal ID="Literal1" runat="server" Text="<%$Resources:mycustomresource,heading_purpose_displayname%>"/>
Is that it? Well, uh, no we need to create language resource files too, but it isn't that bad I promise!
A resource file is an xml based file that resides in 12 hive/Resources. You will see other resource files in that folder that you can copy. For my purposes I need to create three resource files.
One will be called mycustomresource.en-us.resx and holds my English language text.
Next is mycustomresource.fr-fr.resx which will hold my French language text.
Finally there is mycustomresource.resx which will be the default should a page of any other language and locale access the resource. (I'm going to duplicate my English text in there, because it's a fairly common worldwide language and I don't speak Mandarin).
The files themselves (this is the English one) will have the following content in there;
<?xml version="1.0" encoding="utf-8"?>
<!-- _lcid="1033" -->
<root>
<data name="heading_purposetext_displayname">
<value>Purpose</value>
</data>
<data name="heading_processtext_displayname">
<value>Process</value>
</data>
</root>
The LCID is just documentation and corresponds to the locale. (It would be 1036 for France). The 'data name' elements are the keys, and the 'value' elements are... the text value.
So, thinking back to that directive I'm going to put in my page <%$Resources:mycustomresource,heading_purpose_displayname%>"/> it's clear to see that it's just the word 'Resources' followed by the resource file name (without file extension), and the key I specify in my 'data name' elements.
Once those three files have been created it's time to deploy them. Although they are happily sitting in the 12 hive Resources folder, a copy also needs to reside in the App_GlobalResources folder of each web application that will use these resources. This deployment can be done by hand or using stsadm. The stsadm command you use is stsadm –o copyappbincontent with no parameters, but if I demonstrate that it doesn't really tell you what's going on, so I'll cover the manual approach;
Copy the resource files into your web application for all sites you want to use them. The App_GlobalResources folder sits in INETPUB->WWWRoot->WSS->VirtualDirectories-> 'port number for your application' .
Once they're in there you do need to modify them. Sharepoint requires that a RESX schema be specified in the file. I don't mind telling you that this was a litle mystifying when I first came across it. However Microsoft have kindly provided some documentation in existing resource files that explains what the schema is all about and what is possible. Opening an existing cms resource file in the same folder and copying that schema will work for now as a quick fix, but an example schema is posted for you below this post to read and understand. For our example, copy the schema text and paste between the opening root element and the first 'data name' element.
Once that is done, we are there! Inserting literal controls such as <asp:literal ID="Literal1" runat="server" Text="<%$Resources:mycustomresource,heading_purpose_displayname%>"/> will now insert the right language text in our page layout regardless of which variation we're in (as long as you've created a resource file for that Variations locale). Isn't that nicer than all that javascript from Part 1 ? ;)
Example RESX Schema taken from existing cms resource files;
<?xml version="1.0" encoding="utf-8"?>
<!-- _lcid="1033" _version="12.0.4518.1014" _dal="1" -->
<!-- _LocalBinding -->
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
+++
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<Data Name="heading_processtext_displayname">
<Value>Process</Value>
</Data>
<Data Name="heading_purposetext_displayname">
<Value>Purpose</Value>
</Data>
</root>