Localize Strings in Javascript

前端 未结 12 1307
长发绾君心
长发绾君心 2020-11-28 05:52

I\'m currently using .resx files to manage my server side resources for .NET.

the application that I am dealing with also allows developers to plugin Ja

12条回答
  •  有刺的猬
    2020-11-28 06:09

    Well, I think that you can consider this. English-Spanish example:

    Write 2 Js Scripts, like that:

    en-GB.js
    lang = {
        date_message: 'The start date is incorrect',
        ...
    };
    es-ES.js
    lang = {
        date_message: 'Fecha de inicio incorrecta',
        ...
    };
    

    Server side - code behind:

    Protected Overrides Sub InitializeCulture()
        Dim sLang As String 
        sLang = "es-ES" 
    
        Me.Culture = sLang
        Me.UICulture = sLang
        Page.ClientScript.RegisterClientScriptInclude(sLang & ".js", "../Scripts/" & sLang & ".js")
    
        MyBase.InitializeCulture()
    End Sub
    

    Where sLang could be "en-GB", you know, depending on current user's selection ...

    Javascript calls:

    alert (lang.date_message);
    

    And it works, very easy, I think.

提交回复
热议问题