IdentityServer3 icon indicating copy to clipboard operation
IdentityServer3 copied to clipboard

Support for localization of strings in HTML templates

Open nenadvicentic opened this issue 9 years ago • 2 comments

Consider adding support for localization of HTML templates.

Currently, strings like "Username", "Password", "Local Login", etc are hard coded inside of HTML templates. Only way to add translations

<div class="form-group">
    <label for="username">Username</label>
    <input required ... placeholder="Username" ...>
</div>

Can placeholders be added and fed from Resource file(s)? Something like:

<div class="form-group">
    <label for="username">{resources.Username}</label>
    <input required ... placeholder="{resources.Username}" ...>
</div>

...so that we don't have to override HTML templates for translations only?

nenadvicentic avatar Aug 24 '15 14:08 nenadvicentic

+1 for that. The only reason we override the viewservice is to add localization

henninga avatar Sep 09 '15 11:09 henninga

In the CustomViewService class, LoadHtmlMethod : you can add your code for localization like this : private string LoadHtml(string name) { SetCulture(); var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"content\app"); if (System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToLower() == "tr") { file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"content\app\tr"); } file = Path.Combine(file, name + ".html"); return File.ReadAllText(file); }

    private void SetCulture()
    {
        string langName = "tr-TR";
        if (HttpContext.Current.Request.Cookies["ln"] == null)
        {
            if (HttpContext.Current.Request.UserLanguages != null &&
                        HttpContext.Current.Request.UserLanguages.Length != 0)
            {
                langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
            }
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("ln", langName));
        }
        else
        {
            langName = HttpContext.Current.Request.Cookies["ln"].Value;
        }
        CultureInfo ci = new CultureInfo(langName);
        Thread.CurrentThread.CurrentUICulture = ci;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
    }

Do not forget Global.asax

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        string langName = "tr-TR";
        if (HttpContext.Current.Request.Cookies["ln"] == null)
        {
            if (HttpContext.Current.Request.UserLanguages != null &&
                        HttpContext.Current.Request.UserLanguages.Length != 0)
            {
                langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
            }
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("ln", langName));
        }
        else
        {
            langName = HttpContext.Current.Request.Cookies["ln"].Value;
        }
        CultureInfo ci = new CultureInfo(langName);
        Thread.CurrentThread.CurrentUICulture = ci;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);

    }

in the Content/app folder add a new folder like "tr" for turkish for example and take your localized views there

farshid3003 avatar Jan 12 '16 13:01 farshid3003