Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript localization in MVC 5 project #403

Open
marth-santi opened this issue Jul 31, 2020 · 2 comments
Open

Javascript localization in MVC 5 project #403

marth-santi opened this issue Jul 31, 2020 · 2 comments

Comments

@marth-santi
Copy link

marth-santi commented Jul 31, 2020

Please help I search all issues but see no one encounter the same problem as mine.
My project is an MVC 5 with some javascript code behind pages, using jquery ajax to navigate to some cshtml partial component.
Localization using Void url scheme in *.cshtml and *.cs is OK, but when I start using nugget inside string in javascript file ==> the web stuck at my home page, with url still has language postfix ../en or ../vn (behaving like Scheme1), and all elements referred in js file are not displayed / rendered

I dont know how to fix this, having tried:
1/ suspect bundle optimization of js files cause problem ==> set BundleTable.EnableOptimizations = false; ==> no avail
2/ follow this issue ##226 ==> change Script.Render() like @advantiss suggests ==> no avail
3/ Change httpCompression in Web.config (suspecting it to be the problem) ==> no avail too

For more info, the home page still render, but without all elements which got referenced in javascript files

@marth-santi
Copy link
Author

So I found the problem. The i18n module messes with the bundling process of MVC ==> my Javascript files are cached with the old version and if I change the Js files (add nugget, or anything inside,... ), those files will be completely broken, the view cannot find it.

I havent checked the source code (of i18n). But had anyone encountered and solved this problem ?

@vhatuncev
Copy link
Contributor

vhatuncev commented Sep 8, 2020

@marth-santi here is my solution for this issue. I have a global javascript object which accessible from anywhere on client side. On page load it's initialized only with required on particular page translations words. Then in any javascript code I can access translation object and get required translation.
This way will never be affected by cache issue, javascript minification. Also if you need to change translation you will get immediate updates for end users without cache invalidate or new release to production.

Here is my implementation. On server side I have base class for serialization translation objects, as I control which translation goes to the page or control.

public class LangBase
{
        private static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            StringEscapeHandling = StringEscapeHandling.EscapeHtml
        }

        public static HtmlString Serialize(object langObject)
        {
            string serializeObject = JsonConvert.SerializeObject(langObject, JsonSettings);
            HtmlString result = new HtmlString(serializeObject);
            return result;
        }

        public virtual HtmlString ToJson()
        {
            return Serialize(this);
        }
}

This is an example of translation object for specific page

public class ExamplePageTranslation : LangBase
{
        public override HtmlString ToJson()
        {
            var lang = new
            {
                Texts.Name,
                Texts.City
            };

            return Serialize(lang);
        }
}

And finally pass it to page which I need to

<script>
    $(document).ready(function () {
        @{  var lang = new ExamplePageTranslation(); }
        var page = new IndexPage(@lang.ToJson());
    })
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants