Click here to Skip to main content
15,885,000 members
Articles / Programming Languages / Visual Basic

Working with CultureInfo

Rate me:
Please Sign up or sign in to vote.
2.82/5 (10 votes)
27 Sep 2007CPOL 54.5K   460   21   9
Working with CultureInfo using VB.NET.It's simple and easy.Try it now.

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.

VB
' 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:

VB
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:

VB
GetAllLanguages(ddlCountry.SelectedItem.Text)

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

VB
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)


Written By
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? Pin
MartyK200728-Sep-07 2:50
MartyK200728-Sep-07 2:50 
AnswerRe: whats the point? Pin
Ganesan Sankaran28-Sep-07 4:07
Ganesan Sankaran28-Sep-07 4:07 
GeneralRe: whats the point? Pin
MartyK200728-Sep-07 4:14
MartyK200728-Sep-07 4:14 
GeneralRe: whats the point? Pin
Ganesan Sankaran4-Oct-07 2:22
Ganesan Sankaran4-Oct-07 2:22 
QuestionYour download link is not working Pin
Abhishek Sur28-Sep-07 0:46
professionalAbhishek Sur28-Sep-07 0:46 
AnswerRe: Your download link is not working Pin
Ganesan Sankaran28-Sep-07 4:10
Ganesan Sankaran28-Sep-07 4:10 
GeneralRe: Your download link is not working Pin
Abhishek Sur28-Sep-07 4:23
professionalAbhishek Sur28-Sep-07 4:23 
AnswerRe: Your download link is not working Pin
Ganesan Sankaran30-Sep-07 18:18
Ganesan Sankaran30-Sep-07 18:18 
GeneralRe: Your download link is not working Pin
Abhishek Sur2-Oct-07 21:42
professionalAbhishek Sur2-Oct-07 21:42 

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

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