user6440832
user6440832

Reputation: 39

How To translate text on a button in multilingual website in asp.net

I am developing an asp.net Website.I have Resource.resx file to translate term of website. every thing is ok and I want to translate text of button to english and persian when I use Text='<%#Resources.Resource.ارسال %>' for text it doesn't work and does not display text on it.

<asp:Button ID="Button1" runat="server" Text='<%#Resources.Resource.ارسال %>' Width="94px" CssClass="btn btn-primary" OnClick="Button1_Click" />

Upvotes: 1

Views: 826

Answers (1)

Denys Wessels
Denys Wessels

Reputation: 17039

  1. Right click project -> Add -> Add ASP.NET Folder -> App_GlobalResources
  2. Add two resources files to this folder, one for English and another for Persian. I'm using English and French.English is default and the French resource file is called Resource.fr.resx.The name is important so make sure you name yours with the Persian culture.

Adding App_GlobalResources to project

  1. Change your button markup to fetch the text from the resource file:

Like this:

<asp:Button ID="Button1" runat="server" Text="<%$ Resources:Resource, Button1 %>" />
  1. Override the InitializeCulture() method in the code behind and set the culture depending on what type of user is viewing the web application.

Like this:

public partial class GlobalizationExample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void InitializeCulture()
    {
        //Get the language that the client prefers from the browser
        string preferredLanguage = Request.UserLanguages[0];

        //Set the language for the page
        System.Threading.Thread.CurrentThread.CurrentUICulture = 
            new System.Globalization.CultureInfo(preferredLanguage);

        System.Threading.Thread.CurrentThread.CurrentCulture =
           System.Globalization.CultureInfo.CreateSpecificCulture(preferredLanguage);
    }
}

If the above doesn't help you here is a great video on different ways of applying globalization in ASP.NET.

Upvotes: 1

Related Questions