As a part of globalization of an application we will require to set culture depending on the which language client’s browser is using. And using these culture we should represent date, numbers, currency etc in specific format. For this we can use Request.UserLanguages. Following is a method which returns the code of language which the browser is using.
///
/// Gets the current browser language used by the client.
///
/// Returns the culture name generated from the browser language
public string GetBrowserLanguage()
{
string browserLanguage = string.Empty;
HttpRequest Request = HttpContext.Current.Request;
if (Request.UserLanguages == null)
return string.Empty;
try
{
browserLanguage = Request.UserLanguages[0];
if (!string.IsNullOrEmpty(browserLanguage))
{
if (browserLanguage.Length < 3)
browserLanguage = browserLanguage + “-” + browserLanguage.ToUpper();
CultureInfo culture = CultureInfo.CreateSpecificCulture(browserLanguage);
return culture.Name;
}
return string.Empty;
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
return string.Empty;
}
If we want to set culture for RDLC report items then pass this culture code/name to the report. And in report right click->properties, choose code tab. Here we can write simple VB code. You can write a cb function to format the number according the browser culture.
Public Function FormatNumberByCulture(ByVal size As Long, ByVal cultureName As String)
If cultureName.Length > 0 Then
Return size.ToString(“N”, System.Globalization.CultureInfo.CreateSpecificCulture(cultureName))
End If
Return size.ToString()
End Function
Now, call this function from report. In report, right click in the textbox which will display the number. NOw choose expression and call the function in this expression like this,
=Code.FormatNumberByCulture(Fields!Size.Value,Fields!CultureName.Value)
Now the number will be formated with the culture name generated from client’s browser.
Thanks,
Anil.