473,511 Members | 15,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# global object

14 New Member
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
Expand|Select|Wrap|Line Numbers
  1. 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.)
Oct 14 '08 #1
5 3226
Plater
7,872 Recognized Expert Expert
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?
Oct 14 '08 #2
Frinavale
9,735 Recognized Expert Moderator Expert
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)
Expand|Select|Wrap|Line Numbers
  1. using System.Reflection;
  2. using System.Resources;
  3.  
  4. sealed class MyClass 
  5. {    private static ResourceManager _resourceManager;
  6.      public static initialize()
  7.      {
  8.           _resourceManager=New ResourceManage("namespce.resourceFileName", Assembly.GetExecutingAssembly);
  9.      }
  10.  
  11.     public static string GetString(String key)
  12.     {  
  13.        try
  14.        {  String str = _resourceManager.GetString(key);
  15.           return str;
  16.        }
  17.        catch(Exception ex)
  18.        {
  19.           return "";
  20.        }
  21.     }
  22.   }
Make sure to initialize an instance of your Static Class used to manage your resources in your Global.asax Application_Start method.
Oct 14 '08 #3
Zetten
14 New Member
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.langEn.<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 ;-)
Oct 15 '08 #4
Plater
7,872 Recognized Expert Expert
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.
Oct 15 '08 #5
Frinavale
9,735 Recognized Expert Moderator Expert
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.langEn.<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.resx

eg:
  • myResource.resx <--this would be the default resource
  • myResource.fr.resx <--this would be the default French resource
  • myResource.es.resx <---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:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page  .... Culture="en-US" UICulture="auto"%>
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 InitializeCulture method in your aspx page (you cannot override this in your master page).

In the InitializeCulture method you need to set the Thread.CurrentThread.CurrentUICulture and Thread.CurrentThread.CurrentCulture to the language/culture that the user selected.

You'll have to override this in every aspx page in your application (note that the InitializeCulture 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 InitializeCulture method and how to implement Globalization into your asp.net application.

Cheers!

-Frinny
Oct 15 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
8266
by: WhyteWolf | last post by:
I'm trying to set a object as global for access through out the rest of my script ... {a basic SQL accessing object} however if I try calling the object from with in another object it acts as if it...
8
2509
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
9
2364
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
15
2452
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
8
4845
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
16
3196
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
3
2754
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
2318
by: arun1985 | last post by:
In the project i am using i am having the following code and when i upload it to the server.Its givig me the following error in the global.cs file. Server Error in '/' Application. ...
15
2026
by: Bob | last post by:
Is there anyway to access the global object from inside a function other than doing a "var _global = this;" before declaring the function? Thanks
112
5350
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.