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?

Sumon154
  • 1
  • 1
  • 5

2 Answers2

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>
Community
  • 1
  • 1
maztt
  • 12,278
  • 21
  • 78
  • 153
  • 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 – Sumon154 Jan 13 '16 at 14:34
  • You have to check language specific resource file for UserEmailAddress – maztt Jan 13 '16 at 14:35
  • 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 – maztt Jan 13 '16 at 14:38
  • everything is there as i said. It is not working for Model Displayname and Error message validation ... – Sumon154 Jan 13 '16 at 14:53
  • 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"); – maztt Jan 13 '16 at 15:06
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")
   }