Ramji Ramji - 1 month ago 19
Javascript Question

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

Answer

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>