C# global object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zetten
    New Member
    • Aug 2008
    • 14

    C# global object

    I'm writing a website in ASP.NET, with C# doing all the work, but I'm fairly new to the whole Object Orientated way of thinking, so I'm having a few teething troubles.

    I have an external XML file which contains all the static text for the site, with the idea that there will eventually be multiple files for different languages, and all that will need to change is a single declaration of which language to use at a high level.

    I would like the XmlDocument object which accesses the file to be globally accessible by all the different functions and methods across the site.

    How can I go about this? Do I have to make a new public class for all my globals, as some websites seem to suggest? I can declare the object as public in the root of a 'globals' class, using
    Code:
    public System.Xml.XmlDocument objXmlLang = new System.Xml.XmlDocument();
    But I don't know how to go about loading the file, since you can't use objXmlLang.Load () outside of a method. Obviously I only really need to load the document once on each page visit, so how can I do it?

    (I'm not sure on a lot of the terminology and stuff, but hopefully you can understand the general idea of what I'm saying.)
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Hmm, just load it once into a DataTable or something when the application starts up (see global.asax) and then have a public static class provide properties/methods to pull the needed data from the datatable.
    In the future you could load all language variants into the table, and have those same methods take a language value to determine which value to retreive?

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      First off, I'd recommend using resx (resource) files to store your content. These are really easy to use in ASP.NET applications.

      If this doesn't help you...you can create a Public Sealed (or NotInheritable in VB.NET) class that manages your file for you....


      Eg:
      (Please note that C# is not something I use every day and so the following code may not work without some modifications)
      [code=cpp]
      using System.Reflecti on;
      using System.Resource s;

      sealed class MyClass
      { private static ResourceManager _resourceManage r;
      public static initialize()
      {
      _resourceManage r=New ResourceManage( "namespce.resou rceFileName", Assembly.GetExe cutingAssembly) ;
      }

      public static string GetString(Strin g key)
      {
      try
      { String str = _resourceManage r.GetString(key );
      return str;
      }
      catch(Exception ex)
      {
      return "";
      }
      }
      }[/code]

      Make sure to initialize an instance of your Static Class used to manage your resources in your Global.asax Application_Sta rt method.

      Comment

      • Zetten
        New Member
        • Aug 2008
        • 14

        #4
        Thanks for the advice. I've decided to go down the .resx route, as it seems the most intuitive.

        I've created one .resx file (langEn.resx) and started putting the strings in. I can retrieve them with no problems at all directly in the pages by accessing Resources.langE n.<string name>

        This is enough for now, as I only have English to worry about. But in the future I would have to change "langEn" all through the site.

        How can I dynamically change the resource file it looks at? Ideally I'd like to be able to set a variable somewhere (in Session_Start perhaps, from a cookie) which contains the name of the resource for that language (so "langEn" or "langDe" or "langFr") and then access it all through the application.

        In PHP syntax (the dynamic naming bit I mean) I would imagine it would have something like {$langauge} in place of the "langEn" but I don't know how to create a dynamic object (is that even the right terminology?) in C#.

        Thanks for your help so far; hopefully this will be the last bit for this question ;-)

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          There are like "current culture" settings that can effect what resource file to read from.
          I don't remember quite how to do it, but if you search on culture specific settings in .NET you should come up with something.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by Zetten
            Thanks for the advice. I've decided to go down the .resx route, as it seems the most intuitive.

            I've created one .resx file (langEn.resx) and started putting the strings in. I can retrieve them with no problems at all directly in the pages by accessing Resources.langE n.<string name>

            This is enough for now, as I only have English to worry about. But in the future I would have to change "langEn" all through the site.

            How can I dynamically change the resource file it looks at? Ideally I'd like to be able to set a variable somewhere (in Session_Start perhaps, from a cookie) which contains the name of the resource for that language (so "langEn" or "langDe" or "langFr") and then access it all through the application.

            In PHP syntax (the dynamic naming bit I mean) I would imagine it would have something like {$langauge} in place of the "langEn" but I don't know how to create a dynamic object (is that even the right terminology?) in C#.

            Thanks for your help so far; hopefully this will be the last bit for this question ;-)
            Watch your naming conventions.
            Your resources should be named in the following format:

            nameOfResource. LanguageCode.re sx

            eg:
            • myResource.resx <--this would be the default resource
            • myResource.fr.r esx <--this would be the default French resource
            • myResource.es.r esx <---this would be the default Spanish resource
            • myResource.en-CA.resx <--this would be for Canadian English


            See this article for more information on naming conventions and packaging your resources and see this article for a list of cultural/language codes.

            If you set your page directive's UICulture to "auto" the ASPX page will automatically use the resource that matches the preferred culture set in the user's browser.

            eg:
            [code=asp]<%@ Page .... Culture="en-US" UICulture="auto "%>[/code]
            The above page directive sets the culture to default to US English and sets the page to automatically detect the user's browser's cultural settings.

            If you want the user to be able to choose which language resource to use you will have to override the InitializeCultu re method in your aspx page (you cannot override this in your master page).

            In the InitializeCultu re method you need to set the Thread.CurrentT hread.CurrentUI Culture and Thread.CurrentT hread.CurrentCu lture to the language/culture that the user selected.

            You'll have to override this in every aspx page in your application (note that the InitializeCultu re occurs before your page is loaded but after session is loaded so you can store the user's cultural settings in Session if you like....by the way, information stored in Session for a user is normally referred to as persistent data...)

            See this article for more information on the InitializeCultu re method and how to implement Globalization into your asp.net application.

            Cheers!

            -Frinny

            Comment

            Working...