+ Reply to Thread
Results 1 to 2 of 2

Thread: Global Resource Files

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Global Resource Files

    I'm trying to get labels on my site to change depending on the culture set for a particular user.

    The default resource will be en-GB and I have a MyName.resx file with an entry:-
    Code:
    Name      Value
    County    County
    
    I also have a file MyName.en-US.resx with an entry:-
    Code:
    Name      Value
    County    State
    
    In my page I have this label control:-
    Code:
    <asp:Label ID="lblCounty" runat="server" Text="<%$Resources:MyName,County %>" />
    
    And in my Master Page I'm trying to set the culture value of the thread using:-
    Code:
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(user.culture);
            Thread.CurrentThread.CurrentCulture = new CultureInfo(user.culture);
    
    I've tried this in various page event calls (Page_Load, Page_PreRender etc.) but it keeps using the en-US resource and displaying State on the page.

    The culture for the user is set to en-GB and if I write out the display name, directly after setting it as above, using the following code, :-
    Code:
    Thread.CurrentThread.CurrentCulture.DisplayName
    
    it displays English (United Kingdom)

    How do I get the resource files to work correctly?

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. For anyone interested, to get this to work, you have to override the InitializeCulture() method of the System.Web.UI.Page class.

    The way I did this was to create my own base page class that inherits from the System Page class:-
    Code:
    using System;
    using System.Web;
    using System.Globalization;
    
    using Ascent.App.BLL.Users;
    
    namespace Ascent.App.BLL.Pages
    {
        public class BasePage : System.Web.UI.Page
        {
            protected override void InitializeCulture()
            {
                Users.User user = new User();
                user = Users.User.GetUser(Convert.ToInt32(HttpContext.Current.User.Identity.Name));
                System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(user.culture);
                System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(user.culture);
            }
        }
    }
    
    And then have all your pages (or just any pages that are going to use globalization) inherit from this class instead of the System Page class.

    The alternative is to include the InitializeCulture override in each page you need it for. NOTE: You can't include it in a master page as there is no InitializeCulture method in the System MasterPage class to overwrite.

    You then just need .resx files for each culture you're going to provide different information for (resource.en-gb.resx, resource.en-us.resx) and a base .resx file for default (resource.resx). Not sure if you need this last one, but I have it in mine...

    Hope that helps someone.

+ Reply to Thread

Similar Threads

  1. global variable
    By guddu in forum .NET Development
    Replies: 3
    Last Post: September 11th, 2009, 09:20 AM
  2. Excel (take worksheet from 2 files?)
    By Rebelle in forum Microsoft Office
    Replies: 22
    Last Post: April 22nd, 2009, 03:27 PM
  3. Need to automatically backup certian files...
    By bryceowen in forum Visual Basic Programming
    Replies: 2
    Last Post: March 26th, 2009, 07:38 PM
  4. Global Error Class
    By richyrich in forum .NET Development
    Replies: 14
    Last Post: March 23rd, 2009, 11:32 AM
  5. .Net Global Error Handling
    By jmurrayhead in forum .NET Code Samples
    Replies: 4
    Last Post: March 23rd, 2009, 10:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO