Search code examples
durandalhottowelmultilinguali18next

Multi language support in Hot towel(Durandal framework)


I'm in the process of creating a application using Hot Towel, which supports multiple language (eg. english, french)

I have the referred the following links

Translating Views

Durandal localization example

And my question is how can i render views of application based on user language. If user selects english, the complete application should display contents in english. How to achieve this

I have tried something like in the second link.

define({
    'root': {
        welcome: 'Welcome!',
        flickr: 'Flickr'
    },
    'fr-fr': true,
    'es-es': true,
});

Is this i have to create seperate views for languages or i have to create seperate App folder for contents

Suggest me some best practices.


Solution

  • you can do some thing like this . YOu can change the APP folder if you are need do lot of locale changes you can use the following method

    @{
    
            string strAcceptLanguage;
    
    
            strAcceptLanguage = System.Configuration.ConfigurationManager.AppSettings["Locale"].ToString();
    
            if (strAcceptLanguage == "en-us")
        {
             @Scripts.Render("~/Scripts/vendor.js")
            <script type="text/javascript" src="~/Scripts/require.js" data-main="en-US/main"></script>
    
        }
            else if (strAcceptLanguage == "es-es")
        {
             @Scripts.Render("~/Scripts/vendor.js")
            <script type="text/javascript" src="~/Scripts/require.js" data-main="en-UK/main"></script>
    
        }
    
        else      if (strAcceptLanguage == "fr-fr")
        {
             @Scripts.Render("~/Scripts/vendor.js")
            <script type="text/javascript" src="~/Scripts/require.js" data-main="AUZ/main"></script>
    
        }
    
    
    
    }
    

    in the Index.cshtml you can use the above code and for the you need to have value in Webconfig file

    <add key="Locale" value="en-us" />
    

    and in the SPA page each time when the user try to change the locale by pressing button or any kind of option you have to trigger a event to call a AJAX post to assess a REST api to update the Given Locale Value in the webconfig file

    changeLocale: function (val) {
    
                var name = JSON.stringify({
                    locale: val
                });
    
                $.ajax({
                    cache: false,
                    url: "http://localhost:49589/api/Locale",
                    type: "POST",
                    dataType: "json",
                    data: name,
                    contentType: "application/json; charset=utf-8",
                    processData: false,
                    success: function (json) {
    
    
                        alert(json);
                        location.reload();
    
                    },
                    error: function (json) {
                        alert("error" + JSON.stringify(json));
                    }
                });
    

    you have to write the above code in the shell.js and the shell.html has the following code

     <div class="btn-group" data-toggle="buttons-radio">
                    <button type="button" class="btn btn-primary" data-bind="click: changeLocale.bind($data, 'en-us')">English</button>
                    <button type="button" class="btn btn-primary" data-bind="click: changeLocale.bind($data, 'es-es')">French</button>
                    <button type="button" class="btn btn-primary" data-bind="click: changeLocale.bind($data, 'fr-fr')">Japanese</button>
                </div>
    

    And the rest api like this

     [HttpPost]
            public string ChangeLocale(Locale l)
            {
                ConfigurationManager.AppSettings["Locale"] = l.locale;
    
                return "success";
    
            }
    

    hope this will help

    enter image description here

    enter image description here

    enter image description here