简体   繁体   中英

Multilingual alert messages in JavaScript

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM