Multilingual is a concept of changing the language dynmaically based on country. Here I have explained simple example of using multilingual in our application.

First we need to add the App_GlobalResources  folder and then add resource files in our application like below,





Here, the Test.resx file is need for default, then only it works fine.

Then we will assing the key and values as per our requirement.The keys are the pre defined messages, error messages, lables and etc.,

In default.aspx, contains the dropdown with two different inputs. The dropdown selection, we will change the language of the page.

<div>
Language :
<asp:DropDownList ID="ddlLanguage" runat="server"
OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Text="English" Value="en-US" Selected="True"></asp:ListItem>
<asp:ListItem Text="French" Value="fr-FR"></asp:ListItem>
</asp:DropDownList>
&nbsp;--&gt;
<asp:Label ID="lblLocality" runat="server" SkinID="002lblNormal" Text="Locality :"></asp:Label>
&nbsp;<input id="txtLocality" runat="server" class="ctlTextBox" maxlength="50" type="text" />&nbsp;&nbsp;&nbsp;
<asp:Label ID="lblPhone" runat="server" SkinID="002lblNormal" Text="Phone :"></asp:Label>
<input id="txtPhone1" runat="server" class="ctlTextBox" maxlength="15" onkeypress="return ValidateTextBox('txtPhone1');"
type="text" />
</div>


Then we need to allocate the language by using CultureInfo. See the below example,

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
LoadLanguageSelection();
}
}
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
LoadLanguageSelection();

}

//Purpose : Load the Language
private void LoadLanguageSelection()
{
lblLocality.Text = Convert.ToString(GetGlobalResourceObject("Test", "Locality"));
lblPhone.Text = Convert.ToString(GetGlobalResourceObject("Test", "Phone"));
}



The LoadLanguageSelection() method is used to load the page. Within the GetGlobalResourceObject we have given the default Resource file name(this is the identification of resource file with culture name, see the solution explorer in the above figure) and key name.



Now we see the dropdown selection, the page will get change the language itself like below.



Comments (0)