Click here to Skip to main content
Click here to Skip to main content

Working with CultureInfo

By , 27 Sep 2007
Rate this:
Please Sign up or sign in to vote.

Screenshot - CultureTest_on_2007-09-28_113159.jpg

Introduction

I'm going to give you a few tips on how to use the CultureInfo in our program as per your needs.

Using the Code

In this demo project, I have written three methods to display the following things:

  1. All languages
  2. All countries
  3. Languages for a selected country

I have used VB.NET to write the program. If you want to use C# or another language you know, you can use it and change the coding syntax as per your language.

Step 1

Include the namespace "Globalization" in your code as Imports System.Globalization. Include three DropDownLists in the design and give them names as per your needs. Here I have given the names ddlCountry, ddlLanguages, ddlCulture, respectively.

Step 2

Write the following method to display all languages.

' Write the following method to Display the All languages

Public Sub GetAllCountryLanguages()
    Dim ci As CultureInfo
    Dim aL As New ArrayList()
    For Each ci In _
    CultureInfo.GetCultures(CultureTypes.SpecificCultures)
        aL.Add(ci.DisplayName)
    Next ci
    ddlCulture.DataSource = aL
    ddlCulture.DataBind()
End Sub

This one is for getting all countries:

Public Sub GetAllCountries()
    Dim myRI2 As New RegionInfo(New CultureInfo("en-US", False).LCID)
    Dim ci As CultureInfo
    Dim aL As New ArrayList()
    For Each ci In _
    CultureInfo.GetCultures(CultureTypes.SpecificCultures)
        myRI2 = New RegionInfo(New CultureInfo(ci.Name, False).LCID)
        aL.Add(myRI2.DisplayName)
    Next ci
    ddlCountry.DataSource = aL
    ddlCountry.DataBind()
    GetAllLanguages(ddlCountry.SelectedItem.Text)
End Sub

'This method will returns the all the Languages for the country as parameter.
'Pass the value for the method by selecting the Country in the country dropdown.

Public Sub GetAllLanguages(ByVal Country As String)
    Dim ci As CultureInfo
    Dim aL As New ArrayList()
    For Each ci In _
        CultureInfo.GetCultures(CultureTypes.SpecificCultures)
        If (ci.EnglishName.ToString().Contains((Country))) Then
            aL.Add(ci.EnglishName)
        End If
    Next ci
    ddlLanguages.DataSource = aL
    ddlLanguages.DataBind()
End Sub

Step 3

In the Country dropdown, add the dropdown change event and call the method:

GetAllLanguages(ddlCountry.SelectedItem.Text)

Call the rest of the methods in the Page Load event.

Protected Sub Page_Load(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles Me.Load 
    If Not Page.IsPostBack Then 
        GetAllCountryLanguages()
        GetAllCountries()
    End If
End Sub

That is it. Test your application by running it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ganesan Sankaran
Web Developer
India India
I'm Ganesan.S,
Software Engineer
Involved in developing MS applications for last 7 Yrs in VB,VB.NET,ASP.NET,Java Script and C#.NET lately into EPiServer and Ajax.

Comments and Discussions

 
Questionwhats the point? PinmemberMartyK200728-Sep-07 2:50 
AnswerRe: whats the point? PinmemberGanesan S28-Sep-07 4:07 
GeneralRe: whats the point? PinmemberMartyK200728-Sep-07 4:14 
GeneralRe: whats the point? PinmemberGanesan Sankaran4-Oct-07 2:22 
QuestionYour download link is not working PinmemberAbhishek sur28-Sep-07 0:46 
AnswerRe: Your download link is not working PinmemberGanesan S28-Sep-07 4:10 
GeneralRe: Your download link is not working PinmemberAbhishek sur28-Sep-07 4:23 
AnswerRe: Your download link is not working PinmemberGanesan S30-Sep-07 18:18 
GeneralRe: Your download link is not working PinmemberAbhishek sur2-Oct-07 21:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 28 Sep 2007
Article Copyright 2007 by Ganesan Sankaran
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid