开发者

Multilingual alert messages in JavaScript

开发者 https://www.devze.com 2022-12-10 18:59 出处:网络
I have a php Web Application Multilingual, I have a php variable which can tell the current language of the开发者_运维问答 Web Application,

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


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


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"] )


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消