Answered by:
How to know the Application.International Country Code in Vb.net

Question
-
Hi,
In Excel VBA, there is a function called
Application.International to find the CountryCode and based on the CountryCode i will display messages for different languages like English & German.
Now, I am working with VB.Net.
How can i do the same functionality.
Please help me with code.
Thanks
Madhukar
Madhukar
Thursday, November 21, 2013 6:18 AM
Answers
-
Hi,
In Excel VBA, there is a function called
Application.International to find the CountryCode and based on the CountryCode i will display messages for different languages like English & German.
Now, I am working with VB.Net.
How can i do the same functionality.
Please help me with code.
Thanks
Madhukar
Madhukar
Also the below code returns the current systems Culture Name and you can look at this thread to see how to get various information. Note in that thread that the code, in the bottom post, ".Items.Add(geoid.ToString & " (user)" & " .. " & GetGeoInformation(geoid, SYSGEOTYPE.GEO_FRIENDLYNAME))" (after Else in the If statement) returns appropriate information for the system the code is running on. Which will have the geo.id followed by the string "(user)". So out of all the entries it lists on my machine the only one that returns with the string "(user)" is for the U.S. as shown in the bottom image. And I believe the country code is the next line below that.
Option Strict On Imports System.Globalization Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Text = CultureInfo.CurrentCulture.Name End Sub End Class
Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.
- Proposed as answer by KareninstructorMVP Thursday, November 21, 2013 12:26 PM
- Marked as answer by Carl Cai Wednesday, November 27, 2013 1:11 AM
Thursday, November 21, 2013 6:45 AM -
I tend to be lazy and wrap my culture code into a custom namespace i.e.
Namespace My <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> _ Partial Friend Class _Culture ' ''' <summary> ''' Used in SetDecimalSeparator below ''' </summary> ''' <remarks> ''' Set to your culture or another culture ''' </remarks> Private CI As New Globalization.CultureInfo("En-US") Public Function Name() As String Return System.Globalization.CultureInfo.CurrentCulture.Name End Function Public Function DisplayName() As String Return System.Globalization.CultureInfo.CurrentCulture.DisplayName End Function Public Function DateSeparator() As String Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator End Function ''' <summary> ''' Return the Time Separator for current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function TimeSepartor() As String Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator End Function ''' <summary> ''' Return CurrencySymbol for current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function CurrencySymbol() As String Return System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol End Function Public Function DecimalSeparator() As String Return System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator End Function ''' <summary> ''' Override Decimal Separator ''' </summary> ''' <param name="Separator">Character to override current culture decimal separator</param> ''' <remarks> ''' Affects current application, not system. ''' </remarks> Public Sub SetDecimalSeparator(ByVal Separator As String) CI.NumberFormat.NumberDecimalSeparator = Separator System.Threading.Thread.CurrentThread.CurrentCulture = CI End Sub ''' <summary> ''' Return a string array of Abbreviated Day Names for current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function AbbreviatedDayNames() As String() Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedDayNames End Function Public Function AbbreviatedMonthNames() As String() Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames End Function ''' <summary> ''' Returns a string array of day names for the current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function DayNames() As String() Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames End Function ''' <summary> ''' Returns a string array of month names for the current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function MonthNames() As String() Return (From M In System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames Where Not String.IsNullOrEmpty(M)).ToArray End Function ''' <summary> ''' Returns the current month long name for the current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function CurrentMonthName() As String Return System.Text.RegularExpressions.Regex.Replace(Now.ToString("y"), "[^A-Za-z\.]", "") End Function Public Function CultureList() As Globalization.CultureInfo() Return (From T In Globalization.CultureInfo.GetCultures(Globalization.CultureTypes.SpecificCultures) Order By T.EnglishName).ToArray End Function ''' <summary> ''' Returns the current long day name for the current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function CurrentDayName() As String Return Now.DayOfWeek.ToString End Function ''' <summary> ''' Returns the current index of the week ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function CurrentDayIndex() As Integer Return Weekday(Now) End Function End Class <Global.Microsoft.VisualBasic.HideModuleName()> _ Friend Module KSG_Culture Private instance As New ThreadSafeObjectProvider(Of _Culture) ReadOnly Property Culture() As _Culture Get Return instance.GetInstance() End Get End Property End Module End Namespace
Then use it
Console.WriteLine(My.Culture.Name)
Simple list of country codes and names
Dim dt As New DataTable Dim ShownName As New DataColumn With {.DataType = GetType(String), .ColumnName = "ShownName"} Dim Code As New DataColumn With {.DataType = GetType(String), .ColumnName = "Code"} dt.Columns.AddRange(New DataColumn() {ShownName, Code}) Code.ColumnMapping = Data.MappingType.Hidden Dim Items = ( From T In Globalization.CultureInfo.GetCultures(Globalization.CultureTypes.SpecificCultures) Order By T.EnglishName ).ToArray For Each c In Items dt.Rows.Add(New Object() {c.EnglishName, c.Name}) Next
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Proposed as answer by Frank L. Smith Thursday, November 21, 2013 2:43 PM
- Marked as answer by Carl Cai Wednesday, November 27, 2013 1:11 AM
Thursday, November 21, 2013 12:26 PM
All replies
-
Hi,
In Excel VBA, there is a function called
Application.International to find the CountryCode and based on the CountryCode i will display messages for different languages like English & German.
Now, I am working with VB.Net.
How can i do the same functionality.
Please help me with code.
Thanks
Madhukar
Madhukar
Also the below code returns the current systems Culture Name and you can look at this thread to see how to get various information. Note in that thread that the code, in the bottom post, ".Items.Add(geoid.ToString & " (user)" & " .. " & GetGeoInformation(geoid, SYSGEOTYPE.GEO_FRIENDLYNAME))" (after Else in the If statement) returns appropriate information for the system the code is running on. Which will have the geo.id followed by the string "(user)". So out of all the entries it lists on my machine the only one that returns with the string "(user)" is for the U.S. as shown in the bottom image. And I believe the country code is the next line below that.
Option Strict On Imports System.Globalization Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Text = CultureInfo.CurrentCulture.Name End Sub End Class
Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.
- Proposed as answer by KareninstructorMVP Thursday, November 21, 2013 12:26 PM
- Marked as answer by Carl Cai Wednesday, November 27, 2013 1:11 AM
Thursday, November 21, 2013 6:45 AM -
I tend to be lazy and wrap my culture code into a custom namespace i.e.
Namespace My <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> _ Partial Friend Class _Culture ' ''' <summary> ''' Used in SetDecimalSeparator below ''' </summary> ''' <remarks> ''' Set to your culture or another culture ''' </remarks> Private CI As New Globalization.CultureInfo("En-US") Public Function Name() As String Return System.Globalization.CultureInfo.CurrentCulture.Name End Function Public Function DisplayName() As String Return System.Globalization.CultureInfo.CurrentCulture.DisplayName End Function Public Function DateSeparator() As String Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator End Function ''' <summary> ''' Return the Time Separator for current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function TimeSepartor() As String Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator End Function ''' <summary> ''' Return CurrencySymbol for current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function CurrencySymbol() As String Return System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol End Function Public Function DecimalSeparator() As String Return System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator End Function ''' <summary> ''' Override Decimal Separator ''' </summary> ''' <param name="Separator">Character to override current culture decimal separator</param> ''' <remarks> ''' Affects current application, not system. ''' </remarks> Public Sub SetDecimalSeparator(ByVal Separator As String) CI.NumberFormat.NumberDecimalSeparator = Separator System.Threading.Thread.CurrentThread.CurrentCulture = CI End Sub ''' <summary> ''' Return a string array of Abbreviated Day Names for current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function AbbreviatedDayNames() As String() Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedDayNames End Function Public Function AbbreviatedMonthNames() As String() Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames End Function ''' <summary> ''' Returns a string array of day names for the current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function DayNames() As String() Return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames End Function ''' <summary> ''' Returns a string array of month names for the current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function MonthNames() As String() Return (From M In System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames Where Not String.IsNullOrEmpty(M)).ToArray End Function ''' <summary> ''' Returns the current month long name for the current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function CurrentMonthName() As String Return System.Text.RegularExpressions.Regex.Replace(Now.ToString("y"), "[^A-Za-z\.]", "") End Function Public Function CultureList() As Globalization.CultureInfo() Return (From T In Globalization.CultureInfo.GetCultures(Globalization.CultureTypes.SpecificCultures) Order By T.EnglishName).ToArray End Function ''' <summary> ''' Returns the current long day name for the current culture ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function CurrentDayName() As String Return Now.DayOfWeek.ToString End Function ''' <summary> ''' Returns the current index of the week ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Function CurrentDayIndex() As Integer Return Weekday(Now) End Function End Class <Global.Microsoft.VisualBasic.HideModuleName()> _ Friend Module KSG_Culture Private instance As New ThreadSafeObjectProvider(Of _Culture) ReadOnly Property Culture() As _Culture Get Return instance.GetInstance() End Get End Property End Module End Namespace
Then use it
Console.WriteLine(My.Culture.Name)
Simple list of country codes and names
Dim dt As New DataTable Dim ShownName As New DataColumn With {.DataType = GetType(String), .ColumnName = "ShownName"} Dim Code As New DataColumn With {.DataType = GetType(String), .ColumnName = "Code"} dt.Columns.AddRange(New DataColumn() {ShownName, Code}) Code.ColumnMapping = Data.MappingType.Hidden Dim Items = ( From T In Globalization.CultureInfo.GetCultures(Globalization.CultureTypes.SpecificCultures) Order By T.EnglishName ).ToArray For Each c In Items dt.Rows.Add(New Object() {c.EnglishName, c.Name}) Next
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Proposed as answer by Frank L. Smith Thursday, November 21, 2013 2:43 PM
- Marked as answer by Carl Cai Wednesday, November 27, 2013 1:11 AM
Thursday, November 21, 2013 12:26 PM