Multilingual alert messages in JavaScript

杀马特。学长 韩版系。学妹 提交于 2020-01-04 06:05:22

问题


I have a php Web Application Multilingual, I have a php variable which can tell the current language of the Web Application,

I need to validate user inputs in client side, and error messages are shown with JavaScript alerts

for example if the php language variable is "french", I need the alert as "bonjour" if the php language variable is "english", I need the alert as "hello"

any ideas


回答1:


Make some sort of dictionary/array for each language you support and depending on which one, include the relevant file or spit out the relevant part in the dictionary.

<?php $lang = 'fr'; ?>
<script>
messagesDictionary = {
    en: {
    message:'Hi'
    },

    fr: {
    message:'Bonjour'
    }

}

alert( messagesDictionary['<?php echo $lang;?>']['message'] );
</script>



回答2:


Use your own namespace

en.js

MyApp.lang = {
    greeting: "Hello",
    warning: "Attention"
};

de.js

MyApp.lang = {
    greeting: "Hallo",
    warning: "Achtung"
};

Use it like alert(MyApp.lang.greeting) then depending on your php variable include the right .js file in the header




回答3:


The simplest way to do this functionality is given as

var userLang = "<?php echo 'en-Us'; ?>"; 
var Langauges={

"en-US":{
"HelloWorld":'Hi this is Us English'
},
"en-EG":{
"HelloWorld":'Hi this is en English'
},
"en-AU":{

"HelloWorld":'Hi this is Au English'
}
}

alert(Langauges[userLang]["HelloWorld"] )


来源:https://stackoverflow.com/questions/1657220/multilingual-alert-messages-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!