How to handle localization in JavaScript files?

前端 未结 3 1716
感情败类
感情败类 2020-12-15 23:43

I want JavaScript code to be separated from views.
I got the requirement to implement localization for a simple image button generated by JavaScript:

<         


        
3条回答
  •  攒了一身酷
    2020-12-16 00:12

    If you insist on keeping it separate, you could do something like:

    //keep all of your localised vars somewhere
    var title = '{title_from_server}';
    document.getElementById('someImage').title = title;
    

    Remember, if you use JavaScript code to initialize any text of elements, your site will degrade horribly where JavaScript isn't available.

提交回复
热议问题