Click here to Skip to main content
15,892,927 members
Articles / Web Development / IIS

Dynamic Website Multi-languages Management

Rate me:
Please Sign up or sign in to vote.
3.49/5 (40 votes)
18 Aug 20075 min read 80.4K   2.4K   57  
A smart way to manage languages on a multilingual website, using resources and globalization,localization
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;

/// <summary>
/// Class to manage language on the website
/// </summary>
public sealed class LanguageManager
{
    /// <summary>
    /// Default CultureInfo
    /// </summary>
    public static readonly CultureInfo DefaultCulture = new CultureInfo("en-US");

    /// <summary>
    /// Available CultureInfo that according resources can be found
    /// </summary>
    public static readonly CultureInfo[] AvailableCultures;

    static LanguageManager()
    {
        //
        // Available Cultures
        //
        List<string> availableResources = new List<string>();
        string resourcespath = Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, "App_GlobalResources");
        DirectoryInfo dirInfo = new DirectoryInfo(resourcespath);
        foreach (FileInfo fi in dirInfo.GetFiles("*.*.resx", SearchOption.AllDirectories))
        {
            //Take the cultureName from resx filename, will be smt like en-US
            string cultureName = Path.GetFileNameWithoutExtension(fi.Name); //get rid of .resx
            if (cultureName.LastIndexOf(".") == cultureName.Length - 1)
                continue; //doesnt accept format FileName..resx
            cultureName = cultureName.Substring(cultureName.LastIndexOf(".") + 1);
            availableResources.Add(cultureName);
        }

        List<CultureInfo> result = new List<CultureInfo>();
        foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            //If language file can be found
            if (availableResources.Contains(culture.ToString()))
            {
                result.Add(culture);
            }
        }

        AvailableCultures = result.ToArray();

        //
        // Current Culture
        //
        CurrentCulture = DefaultCulture;
        // If default culture is not available, take another available one to use
        if (!result.Contains(DefaultCulture) && result.Count>0)
        {
            CurrentCulture = result[0];
        }
    }

    /// <summary>
    /// Current selected culture
    /// </summary>
    public static CultureInfo CurrentCulture
    {
        get { return Thread.CurrentThread.CurrentCulture; }
        set
        {
            //NOTE:
            //Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-A"); //correct
            //Thread.CurrentThread.CurrentCulture = new CultureInfo("fr"); //correct
            //Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-A"); //correct as we have given locale 
            //Thread.CurrentThread.CurrentCulture = new CultureInfo("fr"); //wrong, will not work
            Thread.CurrentThread.CurrentUICulture = value;
            Thread.CurrentThread.CurrentCulture = value;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader
Vietnam Vietnam



PERSONAL DETAILS


Full Name : Nguyen Thanh Tung
Sex : Male
Date of Birth : October 06, 1983
Nationality : Vietnamese


PERSONAL PROFILE

Trustworthy, efficient, capable work under pressure, outgoing person and capable work in a team.



AREA OF EXPERTISE



    • Proficient in Programming: .Net 1.0/1.1/2.0/3.0/3.5 (C#, VB.Net, ASP.Net,
      AJAX, WCF, WWF), VB4/5/6, ASP, PHP, HTML, CSS, Script, XML.
      Experience with Vs Source Safe, Team System, MsBuild, SVN/CVS. Knowledge about
      UnitTest, Test-Driven development.
    • Experience with Databases: SQL Server 2000/2005, MySQL
      4.x/5.x/6.x, Oracle, PostgreSQL, Ms. Access.
    • Experience in EIS: ERP, CRM, SCM ...
    • Experience with Microsoft Products: CRM3/4, Sharepoint
      2003/2007 ...
    • Proficient in OS: Linux (Redhat, Fedora, Mandrake, Suse), Windows (2000/2003
      Server, XP, Vista).
    • Proficient in Graphics (Flash, Photoshop, 3DMax …); Microsoft Office, Visio,
      Microsoft Project.
    • Proficient in Network Devices: Router, Switch, Firewall Server, Mail Server,
      FTP Server, DNS, VPN.
      Experience in Servers: IBM, HP, Sun, Dell ...
    • Familiar with OO and SOA concepts.
    • Knowledge about project management: CMMI, Agile and Scrum, Software
      Engineering, System Analysis and Design, IT Project Management. 
    • Knowledge in Accounting and Finance: Financial Statement, Income
      Statement ...




    MAIN PROJECTS DONE IN THE PAST

    • SMS Gateway.
    • POP3-IMAP-SMTP App.
    • Products Management Sys.
    • Hedge Fund Management Sys.
    • Motion detection.
    • 3D games engine (C# using OpenGL Lib).
    • Video Conference.
    • Information Retrieval.
    • Microsoft CRM3/4
    • Microsoft Sharepoint Portal 2003/2007
    • CMS
    • E-Commerce


Comments and Discussions