Sunday, August 11, 2019

How to create multilingual application in asp.net

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Webpage and Design for make a form multilingual.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.


HTML Code 
  1. <div style="text-align:right;">
  2. <asp:DropDownList ID="ddLang" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddLang_SelectedIndexChanged">
  3. <asp:ListItem Value="en-US" Text="English" />
  4. <asp:ListItem Value="fr-FR" Text="français" />
  5. <asp:ListItem Value="hi-IN" Text="हिंदी" />
  6. </asp:DropDownList>
  7. </div>
  8. <table>
  9. <tr>
  10. <td><asp:Label ID="lblName" runat="server"/></td>
  11. <td><asp:TextBox ID="txtName" runat="server" /></td>
  12. </tr>
  13. <tr>
  14. <td><asp:Label ID="lblAddress" runat="server" /></td>
  15. <td><asp:TextBox ID="txtAddress" runat="server" /></td>
  16. </tr>
  17. <tr>
  18. <td><asp:Label ID="lblState" runat="server" /></td>
  19. <td><asp:TextBox ID="txtState" runat="server" /></td>
  20. </tr>
  21. <tr>
  22. <td><asp:Label ID="lblCountry" runat="server" /></td>
  23. <td><asp:TextBox ID="txtCountry" runat="server" /></td>
  24. </tr>
  25. <tr>
  26. <td></td>
  27. <td>
  28. <asp:Button ID="btnSave" runat="server" /> &nbsp; &nbsp;
  29. <asp:Button ID="btnCancel" runat="server" />
  30. </td>
  31. </tr>
  32. </table>

Step-3: Add a ASP.NET Folder "App_GlobalResources" for save language resource file

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > Add ASP.NET Folder > App_GlobalResources.


Step-4: Add Resources file for save different language under "App_GlobalResources" Folder

Right Click on "App_GlobalResources" Folder form Solution Explorer > Add > New Item > Select "Resources File" under Visual C# > Enter Name (Like "Lang.en-US.resx") > Add. 
[N:B: Please take care of the file Name.] 
Here I have added 3 file for 3 languages. 

LanguageFile Name
EnglishLang.en-US.resx
FrenchLang.fr-FR.resx
HindiLang.hi-IN.resx

Step-5: Change Build Action to "Embedded Resource" of added resource files (3 added files here)

Select Resource file > Right Click go to Property> Change Build Action to Embedded Resource. 

Step-6: Write code into page load event for show selected language.

Write below code into Page_Load event. 
We need to import following Namespace first.. 

  1. using System.Resources;
  2. using System.Globalization;
  3. using System.Threading;
  4. using System.Reflection;

  1. ResourceManager rm;
  2. CultureInfo ci;
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5. if (Session["Lang"] == null)
  6. {
  7. Session["Lang"] = Request.UserLanguages[0];
  8. }
  9.  
  10. if (!IsPostBack)
  11. {
  12. LoadString();
  13. }
  14. }

Here is the function...

  1. private void LoadString()
  2. {
  3. Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["Lang"].ToString());
  4. rm = new ResourceManager("MultiLingual.App_GlobalResources.Lang", Assembly.GetExecutingAssembly());
  5. ci = Thread.CurrentThread.CurrentCulture;
  6.  
  7. lblName.Text = rm.GetString("Name", ci);
  8. lblAddress.Text = rm.GetString("Address", ci);
  9. lblState.Text = rm.GetString("State", ci);
  10. lblCountry.Text = rm.GetString("Country", ci);
  11. btnSave.Text = rm.GetString("Save", ci);
  12. btnCancel.Text = rm.GetString("Cancel", ci);
  13. }

Step-7: Write code into "ddLang_SelectedIndexChanged" event for show selected language.


  1. protected void ddLang_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. Session["Lang"] = ddLang.SelectedValue;
  4. LoadString();
  5. }
Step-8: Run Application.  

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...