MVC Forum as a toolbox
In the last post I talked about using MVC Forum for data access to your own custom data. This time I'll show you another nice little feature you might want to use in your own code.
Even though a lot of the content on the internet these days are in English, and we have Google Translate, it still very much makes sense to localise an application like a forum. More and more people get internet access and not everybody speak or read English.
The latest code on Codeplex does no longer include the localisation DLL, instead I've included a DLL from the content managerment system Umbraco (ASP.NET MVC with their upcoming version 5 release). Included in this DLL is the original localisation code done by @nielskuhnel, quite brilliant by the way, and that has nothing to do with the fact that I've had a tiny bit to do with a small part of it (handling timezones).
So why not just use resource files?
Resource files are too simple, which makes it too complex to use them for the task at hand. Let's take an example, we need to output this text: "1 topic". This seems very simple, and could be handled using this text with String.Format: "{0} topic(s)". Not quite actually. We want the forum to be easily translated into any language, so what would this text look like in Arabic? If we had 1 topic it would be: "موضوع واحد", but if we had 0 or 2 or more, it would be "{0} مواضيع".
That was a simple example, and could easily be handled with a few extra texts in a resource file, but imagine what would happen with a longer text, the complexity would be too much to handle, and that would either make the application extra complex whenever a text was needed or make the texts too generic.
That's why I've included Niels' framework in my project. Let's start off with creating a XML file in a project where we need localised texts. The XML file must be called LocalizationEntries.xml. Select the file in the Solution Explorer, in the properties window under Build Action select Embedded Resource. Now for the format of the XML:
<?xml version="1.0" encoding="utf-8" ?>
<Localizations>
<Namespace Name="mvcForum.Web">
<Text Key="Paging.Topic">
<Translation Language="en-GB"><![CDATA[#MessageCount:{1:1 post|{#} posts} • Page {PageIndex} #PageCount:{>1: of {#}|: of 1}]]></Translation>
<Translation Language="da-DK"><![CDATA[#MessageCount:{1:1 besked|{#} beskeder} • Side {PageIndex} #PageCount:{>1: af {#}|: af 1}]]></Translation>
</Text>
</Namespace>
</Localizations>
And the code that will output the proper text:
ForumHelper.GetHtmlString("Paging.Topic", new { MessageCount = Model.MessageCount + 1, PageIndex = Model.Page, PageCount = Model.Pages })
It might look a bit complex at first glance, but let's walk through it. First off, it will be easier to recognize the parameters in the string, we can call them by the name they were given in the call to the GetHtmlString method. No need to keep track of the index of the parameters like we do with String.Format. The first part, for MessageCount, if 1 output "1 post" otherwise the output will be "{#} posts" where {#} is the value of MessageCount. The PageIndex parameter is just regular output. The last part is pretty much like the first part, for PageCount, if more than 1 output " of {#}" where {#} is the value of PageCount, otherwise output " of 1".
If you want to use this in your own code, you'll need to use another method for getting the text. You need to tell the framework which namespace to read the texts from, so instead you should use these methods (all located in the static ForumHelper class):
public static String GetString<T>(String text, Object values) public static MvcHtmlString GetHtmlString<T>(String text, Object values) public static String GetString(String text, Object values, String namespc) public static MvcHtmlString GetHtmlString(String text, Object values, String namespc)
For the generic parameter you can use any class in the same namespace as you use in the XML file, or you can use any text string as namespace using the 2 other methods.
Categories: MVC Forum - Localisation - Internationalisation
Posted by Steen F. Tøttrup on 10 October 2011 21:03. There are 0 comments.
Write new comment
