Working with CultureInfo






2.82/5 (9 votes)
Working with CultureInfo using VB.NET.It's simple and easy.Try it now.
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:
- All languages
- All countries
- 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
DropDownList
s 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.