Reputation: 5953
Main question:
Why use custom localization helpers if there is something built-in doing the same already?
Long story
Currently I have been reading many options to localize your asp.net mvc website. Most of the posts are old, from oktober 22 2008 for instance.
I think one of the most linked option is the following: Matt Hawley on eWorld. This option creates an Html helper which can be used with
Html.Resource("ResourceName")
Html.Resource("GlobalResourceFileNameWithoutExtension, ResourceName")
for local and global resources. Other use the
<asp:label meta:resourcekey="lblNameResource1" runat="server"/>
instead of
<label></label>
Some problems I had while trying the methods I found were when using <.asp:labels> my partial pages recieve some pretty errors like on my partial page rendering:
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or
cluster, ensure that <machineKey> configuration specifies the same validationKey and
validation algorithm. AutoGenerate cannot be used in a cluster.
In the end, I am wondering, why all this trouble if there are standard methods like:
<%= GetLocalResourceObject("lblNameResourceKey") %>
What are the downsides of using the built in functions? Of course I am not happy with having to use string keys but all methods use them so I think that is inevitable in the end. The only downside I can think of is that every string needs its key, while using asp:labels have some autofunctions builtin (like lblName.Text / lblName.ToolTip etc). But than why is this Matt Hawley so rumored? He uses the exact same approach as the built-in GetLocalResourceObject, only naming it differently with his own helper methods?
Or Am I missing something?
Upvotes: 0
Views: 1950
Reputation: 46
You can check this tutorial too. Create ASP.NET MVC localization. It looks like simple solution and you should twink it if you need form labels, validation or something more complicated
Upvotes: 0
Reputation: 3996
You can also take a look here ASP.NET MVC 2 Localization complete guide and ASP.NET MVC 2 Model Validation With Localization these entires will help you if you working with ASP.NET MVC 2.
Upvotes: 0
Reputation: 5953
Just for people with the same interests as when I asked my question I will put down what I currently have chosen to use as method.
Since I am a very bad typer and always misstype and badly remember the names of my variables I def. need strong typed resources. So what I did was creating global resources. But since I did not like to have one big file (commonly named Strings in many examples) I have chosen to make a few global resource files. In my case I have three for now:
Now, since I develop a dynamic filled website with not that many static text I usually have a variable "ViewPageNameMainText" and some things like "ControllerViewPageName[IDHERE] in my Strings.resx, some vars like "username" "password" in my Forms.resx and some vars like "back" "forward" etc in my ActionLinks.resx.
I have made a simple helper to replace "\r\n" with "\r\n" and some more (though I found out later that
etc do work in resource files). Links in blocks of text I load by passing them as arguments in the string so I get something like below.Html.HtmlStringEncode(string.Format(Resources.Strings.HomeAboutMainText,
new object[]{ Html.ActionLink(Resources.ActionLinks.back, "Home", "Index"),
Html.ActionLink(Resources.ActionLinks.create, "Vacancies", "Create")}))
With in Strings.HomeAboutMainText something like (the {0} {1} etc are replaced by actionlinks in this case):
<.h2>Lorem ipsum dolor<./h2> sit amet {1}, consectetur adipiscing elit. <.p>Aliquam nec libero neque, eget tristique sapien. Praesent lacinia ultricies diam, eget vehicula leo malesuada a. Duis porttitor tincidunt malesuada. Phasellus malesuada eros eu justo dapibus quis molestie ante posuere.<./p> Suspendisse condimentum, sapien id condimentum vulputate, libero justo rutrum sapien, in porttitor tellus metus eu felis. Etiam vitae mollis nisi. Aliquam rutrum nibh vel orci varius feugiat. Praesent at sem arcu, vitae adipiscing eros. Ut vitae massa justo. Donec sit amet mauris sed leo pellentesque feugiat. Nulla facilisi. Cras viverra pulvinar odio nec venenatis. Quisque accumsan cursus interdum. Aliquam consequat tristique mattis. {0}
The dymanic content (mainly newsposts, columns and vacancies) I have given a "language" column in the database which is set at creation of the object. When people change the page language to their language pref. I just get all posts/vacancies etc with language=pref.language and show them on the pages where needed.
Although I am still not very happy It is enough for now. I'm still considering what to do with images but since I have none which need to change atm. that is a problem which does not need to be solved right now.
Upvotes: 0
Reputation: 8755
If you refer to Matt's post (more accurately the post prior to the more popularly linked one!) you'll see the reason he's added those HTML helpers is that he wanted to design something that delegates the responsibility of choosing the local resource location to the ViewEngine (i.e. if you look at the code you see that his subclassed ViewEngine puts the view location in ViewData). This helps with situations where the Views are stored in a database or something similarly non-standard or don't even derive from the Page class.
If you're only ever going to use the WebFormViewEngine it might not be worth losing much sleep over and something like the following should work fine (as you mention in your question)
<%= GetLocalResourceObject("lblNameResourceKey") %>
The second method you give uses a serverside Label control. I'd guess the reason why you're getting errors is that a dependency on ViewState is sneaking in somewhere and as MVC does not emit ViewState the page is going to error.
Regarding the downsides of using ASP.NET GetLocalResourceObject and it's Global equivalent:-
Downsides
Upsides
Which needs to be contrasted with strongly typed resources generated by Visual Studio that Craig mentioned in his answer:-
Downsides
Upsides
There's always tradeoffs associated with these things so it's best to choose what's most suitable for your situation (e.g. what your deployment strategy is, the amount of change you expect in the string resources)
Upvotes: 3
Reputation: 126547
I disagree that "all methods use strings".
We do:
<%= ApplicationName.Properties.Resources.lblNameResourceKey %>
Obviously, you can reference the namespace so that you don't have to fully qualify.
Upvotes: 3