locked
How the heck do you get Localization working? (2.0) RRS feed

  • Question

  • User-1524452824 posted

    I have spent hours reading up on the 2.0 way of localization, and experimenting in my web app, and cannot for the life of me get any of this working at ANY level.  Please help!

    Sample Project:
    HelloWorld
    -App_GlobalResources
    --Main.resx
    -App_LocalResources
    --Test1.aspx.resx
    -Test1.aspx

    Assume that inside Main.resx and Test1.resx I have an item named "Hello.Text" with the value "Hello World"

    Now, in my Test1.aspx page ALL of these methods do not work.  I get a null reference exception:

    CodeBehind:
    Label1.Text =
    this.GetLocalResourceObject("Hello.Text").ToString();
    Label1.Text = this.GetGlobalResourceObject("Main", "Hello.Text").ToString();

    In-liine:
    <asp:Label ID="Label1" runat="server" Text="<%$ Resources:Hello %>"/>

    Strong Type:  How do you use it?  Where the heck is it?  This is not possible whatsoever from within my page:

    string foo = Resources.Main.Hello;
    string foo = System.Resources.Main.Hello;

    Intellisense shows me nothing.  Nothing is available or accessible.  "Resources" is not available in the page class... how is this all mapped?

    This is all so completely broken and frustrating.  I have followed instructions on how to do this with extensive attention to detail using many different excellent reference guides.  My browser is on the correct default language.  Am I missing some stupid thing, or is my .NET framework or Visual Studio just completely freaking hosed?  This should not be so difficult.  It's acting as if my resx files dont exist, even though they do.

    Saturday, February 18, 2006 5:32 PM

All replies

  • User153155060 posted
    I gave this a try and found it works, but not quite as I expected.  Having said that, I think I'm having more luck than you are.

    For me, I get the following behavior with my VS 2005:

    This works against the resource file for my current aspx file (default.aspx, default.aspx.resx):

            Button1.Text = GetLocalResourceObject("lblNameResource1.Text").ToString();

    This works in referencing the global resource (Resource.resx).  Intellisense works here:

            Button1.Text = Resources.Resource.ButtonMessage;  // ButtonMessage is the resource key

    Following, however, does not work.  It always returns null.

            Button1.Text = GetGlobalResourceObject("Resource", "ButtonMessage").ToString();

    Since the second and third methods are identical, I can accomplish everything I need.  As for why it's not working for you, I haven't a clue.
    Sunday, February 19, 2006 1:47 AM
  • User1183903743 posted

    Intellisense shows me nothing

    Did you build the project first? Sometimes it won't do it automatically but will prompt you in the warnings list to generate the resource class by just clicking on it.  Intellisense draws the schema from the code generated class. The other reason is if the files are in the wrong place, especially outside the special folder.

    null reference exception
    That is because your code is looking for a global resource Test1.resx but there isn't one - you only did a local resource for it. There is a fallback mechanism so if a variant(another language resource is missing) it looks for it in the invariant one. However, if you are missing the invariant resource it will throw a null reference exception.
     

    Assume that inside Main.resx and Test1.resx I have an item named "Hello.Text"
    They are not the same

    It looks like you are testing local resources with Test1.aspx, and there is a control(is it a label?) with a property Text . You need to run the Generate Local Resources utility to create the meta tags in the control on the page and it also copies the literal to the resx. From that point on, the value that is in the page markup is ignored. You can even remove the text from the page AFTER running the GLR.

    Now, you have it a bit muddled in the global resource. You are not allowed dots in the global key.(actually you are but it delimits a nested namespace) Try it simpler first with no namespaces Name the key Hello_Text

    Also, the code generated class is in the Temp aspnet folder

    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

    It is a cryptically named file with an extension .cs You can find it with some ingenuity by searching for Hello_Text literal It has to be outside the project until it is compiled. The first thing you will see is that it is not in the System namespace, just Resources namespace.

    So that is the issue with

    string foo = System.Resources.Main.Hello;

    if you had compiled properly you should have gotten something like this:

    string foo = Resources.Main.Hello.Text

    Note that the IDE assumed that the resource name is Text and that Hello was just a namespace nested inside the Main class which itself is inside the Resources namespace.

    If you tried with a simple key it should work easier:

    string foo = Resources.Main.Hello_Text;

    //or
    string foo = this.GetGlobalResourceObject("Main", "Hello_Text").ToString();

    likewise with the inline expression.

    If you do these samples(they all work) you will have a good idea.

    http://beta.asp.net/QUICKSTART/aspnet/doc/localization/localization.aspx

    If you are still having problems, zip your test project (errors and all) and email it to me.

    Sunday, February 19, 2006 7:55 PM
  • User-1524452824 posted

    Thanks for the response.  Excellent info.  Here is my status now:

    I removed the.Text from the resource, so I now have Hello_Text to make it simple.  I did make sure to compile.  I checked the Temporary ASP.NET folder for files containing my "Hello_Text" and found nothing.  And my intellisense has no "Resources" namespace whatsoever, not even the root itself.  If  I begin to Type "Reso" in say the Page_Load funtion, Intellisnse gives me only the following items with the word "resource" in them as show below.

    ResourceManager
    ResourceReader
    ResourceSet
    ResourceWriter

    ...but no "Resource" namespace.  I cannot even begin to access global resources this way.

    and  I have tried all the samples at
    http://beta.asp.net/QUICKSTART/aspnet/doc/localization/localization.aspx

    and none of them work, even if I copy/paste the exact code.  I get null reference, and/or cannot compile because there is no such thing as "Resources" namespace at the page level.  

    However, when I built a test project with nothing but the code from the quickstart samples, everything worked fine.  the Resources Namespace showed up, and everything worked.  But none of this works in my other project I am trying to add localization to...

    My Doom:
     It is a DotNetNuke 4.0.2 project.  And what I noticed is that it violates a lot of the rules for .NET 2.0 localization because it was a crappy port from 1.1.  Sooo.....  I think I am screwed.  I think what is happening is even if I structure my resources properly, it wont compile them because of other errors elsewhere, and I end up getting no Resources Namespace generated, and cannot even use local resources for whatever reason.  I suppose I am stuck untill the DNN team cleans up the .NET 2.0 version of DNN.

    Monday, February 20, 2006 6:57 PM
  • User-1524452824 posted

    On a slight tangent from all this...

    ASP.NET projects have this cool localization stuff.  But how can one use String Typed Localization in a .NET 2.0 Class Library? 

    I created a class library called ClassLbrary1  and I added properly structured resources into it with an App_GlobalResources folder.  I compile it, but I get no Resources namespace.  nor do I have access to things like GetGlobalResourceObject.  Is there a way to do this, using similar "cool tools" found in ASP.NET?  Or do I have to use the old method of building a resource manager?  

    Monday, February 20, 2006 7:05 PM
  • User153155060 posted
    I think the "Resources" namespace shows up on intellisense only if you have created at least one global resource file within the reserved folder App_GlobalResources.  Mine is named Resource.resx residing under the folder App_GlobalResources, so my intellisense shows Resources.Resource.  As for the local resource files, the folder App_LocalResources is the default.
    Monday, February 20, 2006 7:38 PM
  • User1183903743 posted

    I am not a huge fan of DNN. I have heard that Rainbow pride themselves in localization, but have never worked with it.

    Anyway, v2.0 offers out of the box tons of goodies and you don't have to depend on other frameworks to get their act together.

    Monday, February 20, 2006 7:49 PM
  • User1183903743 posted

    It is not a project thing. It is an application thing.

    GetGlobalResourceObject(...) is a static member of the HttpContext object of a web app. It is just exposed for convenience in the page class(oops kitchen sink class). But you can always use it in a non-strongly typed manner in your lib.

    The auto-generation is only for the application. It not only generates the code; based on the file name inside the special folder it also generates the satellite assemblies for you.

    You can always add the global resx to App_GlobalResources along with referencing the lib, a bit of an extra step but not too bad. This is where that namespace thing (which is BTW totally undocumented and confusing to figure out) would come in handy.

    Monday, February 20, 2006 8:31 PM
  • User159848876 posted

    This is a short video that introduces the new resources model in ASP.NET 2.0; touching on App_LocalResources and App_GlobalResources.

    ASP.NET HOW DO I Video Series: Localization

    Hope this helps

    Thursday, February 23, 2006 10:06 PM