Sumon154
Sumon154

Reputation: 1

Changing Current Localization of Asp.net MVC project

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?

Upvotes: 0

Views: 3044

Answers (2)

sagar_bhoi_188
sagar_bhoi_188

Reputation: 14

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")
   }

Upvotes: 0

maztt
maztt

Reputation: 12294

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>

Upvotes: 0

Related Questions