views:

434

answers:

5

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?

+1  A: 

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.

Craig Stuntz
True, have seen it before. But in the end my main question stays the same. Why all these blogposts if it does not really add to the original built-in thing?
bastijn
Your question is why people propose less-than-ideal solutions on their blogs? It wouldn't be a blog if they didn't! :)
Craig Stuntz
My question is if there is a difference between their solutions and the build-in function. Why do they bother writing it. Do I miss something or is it merely for code convenience (nice method names?). Or does it add extra functionality.
bastijn
+2  A: 

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

  • Lots of strings for each resource key, no strong typing / error highlighting.

Upsides

  • Lots of flexibility, can deploy resx files without a recompile of the whole application.

Which needs to be contrasted with strongly typed resources generated by Visual Studio that Craig mentioned in his answer:-

Downsides

  • Changing a string will force a recompile of the application and a redeployment of the application DLLs.

Upsides

  • Strongly typed so no magic strings cleaner code in views due to no ASP.NET localization black magic.

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)

sighohwell
A: 

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:

  • ActionLinks : containing all action link strings
  • Strings : containing all main textblocks on the website
  • Forms : containing all form labels etc used in forms

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.

bastijn
A: 

Hi, 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.

fyasar
A: 

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

Vladimir