0

I am working on a Asp.net mvc webSite where the requirement is the user should be able to change language and the page content should be translated automatically. Problem Is : I have used Custom Attribute in My Model for displaying localization like thus : [CommonCustomAttributes.LocalizedDisplayName("Register", "UserEmail")].

LocalizedIdsplayname is Implemented like following: [AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] public sealed class LocalizedDisplayNameAttribute : DisplayNameAttribute { public LocalizedDisplayNameAttribute(string resourceFileName, string keyName) : base(GetMessageFromResource(resourceFileName, keyName)) { }

        private static string GetMessageFromResource(string resourceFileName, string keyName)
        {
            try
            {
                return CustomLocalizationUtility.GetKeyValue(HttpContext.Current, resourceFileName, keyName);
            }
            catch (Exception)
            {
                return "";
            }

        }
    }

So when i switch between languages it does not change .I mean the displayname of the Model where it is used like @Html.LabelFor(m => m.UserEmailAddress, new { @class = "col-md-3 control-label" }).

But in the .cshtml where i have used @placeholder = @CustomLocalizationUtility.GetKeyValue("Register", "EnterEmail") that works fine when i change language. Am i missing something Serious? I have used Cookie for setting current culture and i have also overidden the BeginExecuteCore method in base controller. I have set the Cookie for Current culture from a Action method. Do we need to add extra javascript for JQuery Obstrusive validation and Model Displayname change based on the current culture change?

2 Answers 2

0

It will not work unless you change the culture in your application when changing the lang and before calling the above label i.e UserEmailAddress. You can create an actionfilter attribute and call it in your controller.

This is the code from my application

   public static List<languages> Availablelanguages = new List<languages>() 
    {
        new languages{ LangFullNmae="English" , LangCultureName="en" },
        new languages{ LangFullNmae="دری" , LangCultureName="prs-AF" },
        new languages{ LangFullNmae="پښتو" , LangCultureName="ps-AF" }
    };

    public static string GetDefaultlanguage()
    {
        string lang = Availablelanguages[0].LangCultureName;
        var cultureInfo = new CultureInfo(lang);
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
    //    HttpCookie langCookie = new HttpCookie("culture", lang);
    //    langCookie.Expires = DateTime.Now.AddDays(2);
        return lang;
    }

These resources can be of further help

It discuss all the places where you can set your culture

Best place to set CurrentCulture for multilingual ASP.NET MVC web applications

Another one

Set Culture in an ASP.Net MVC app

Update

 <tr>
            <td>
                @Resource.DeputyMinisterText
            </td>
            <td>

                @Html.EditorFor(model => model.DeputyMinister
  , new { htmlAttributes = new { style = "dir:ltr;text-align:left" } })
                @Html.ValidationMessageFor(model => model.DeputyMinister)
            </td>
        </tr>
Sign up to request clarification or add additional context in comments.

5 Comments

I have changed the culture during changing the language . The localization call in razor works fine but the model displayname and validation error messages are not being changed though the culture has been changed
You have to check language specific resource file for UserEmailAddress
If the culture is changed you have pass the first step. the second step is you have to have all the translations in the resource file
everything is there as i said. It is not working for Model Displayname and Error message validation ...
another thing you can try is to put on the top of the view you are rendering in case anything is wrong System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("your language");
0

For Localization

1)Make the Resource File and add key value pair.

e.g

 Resources.resx //Make this file Public
 Resources.fr.resx

2)Set the Culture for language (In lang You have To pass LanguageCulture)

    public string SetLanguage(string lang)
    {
        try
        {
            if (!isLanguageAvailable(lang))
            {
                lang = getDefaultLanguage();
            }
       
          var cultureinfo = new CultureInfo(lang);

          Thread.CurrentThread.CurrentUICulture = cultureinfo;
Thread.CurrentThread.CurrentCulture=CultureInfo.CreateSpecificCulture(cultureinfo.Name);
             
          HttpCookie langCookie = new HttpCookie("culture", lang);

          langCookie.Expires = DateTime.Now.AddYears(1);

          HttpContext.Current.Response.Cookies.Add(langCookie);

        }
        catch (Exception)
        { 
        
        }
        return lang;
    }

3)Now you can call the Resource in the View this is the two ways to call the Resource using Key Value

  @{Resources.Add_Value
   ResourcesIndex.ResourceManager.GetString("Add_Value")
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.