views:

188

answers:

2

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  A: 

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>
meder
Wouldn't it be better to store the dictionary server-side so that the client doesn't have to download a whole dictionary including messages that he will never see?
Tom
+4  A: 

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

jitter
Thanks for your solution
Ramji