views:

22

answers:

2

I have a javascript confirm. My text is in english, but if I will change language/culture of my asp.net mvc project confirm is of cource still english.

I can controll it manually and just write two javascript methods (for both Languages) but it is no so clean I think. And if I will have more than 3 languages its will be more dirty.

Could you give me some Tip how can i solve my problem?

Best for me is to have confirm with some metatag or something where can i give my text from resource files out.

+1  A: 
<script language="javascript">
var confirmMessage = "<%= localizedConfirmMessage %>";

// use confirmMessage when showing confirm popup
...
</script>
Alex Reitbort
That is what I normally do, so I know it works :)
Nealv
A: 

You can do seperate js file with strings only, e.g.:

var MultilanguageStrings = {
    'savingConfirmation': {
        'en': 'Do you want to save changes?',
        'pl': 'Czy chcesz zapisać zmiany?'
    },
    'fatalError': {
        'en': 'Fatal error occured.',
        'pl': 'Wystąpił krytyczny błąd.'
    },
    'get': function (key, lang) {
        if (this[key] === undefined)
            return 'Error. There is no such message.';
        if (lang == 'en' && this[key]['en'] === undefined)
            return 'Error. There is no such message.';
        if (this[key][lang] === undefined)
            return this.get(key, 'en');
        return this[key][lang];
    }
};

And than make dynamically generated js file with information which language is set:

var currentLanguage = 'en';

You can also get this information by different way (e.g. AJAX) but solution above has this advantage, that your code is already loaded and currentLanguage is set.

Then in your code put:

...
if (confirm(MultilanguageStrings.get('savingConfirmation', currentLanguage))) {
...

You may want to wrap everything into 1 global variable, that would be more neat. Also remember not to overwrite method MultilanguageStrings.get.

pepkin88