从源头阅读语言

时间:2011-02-17 13:40:22

标签: javascript jquery html if-statement

我想在多语言(en,ru,lt)网站中使用灯箱,所以我需要翻译灯箱。 要为灯箱设置正确的参数,我需要阅读以下语言定义的语言:

    <body>
        <div id="main">

    <div id="logo"> 
        <a href="/en/" ...

因此,如果我在另一种语言标识链接将是该语言代码。我有这个.js代码:

$(function() {
    $('.object').lightBox({
        txtOf: 'translation',
        txtImage: 'another_translation'
    });
});

我应该如何改变JS进行检查?

2 个答案:

答案 0 :(得分:3)

$(function() {
    // Define language map
    var languageMap = {
      en: {txtOf: 'Something', txtImage: 'This is an image'},
      ru: {txtOf: 'что-то', txtImage: 'его является изображение'}
    };
    // Detect language from the logo images href attribute
    var re = new RegExp('/([^/]*)/*');
    var href = $('#logo a').eq(0).attr('href');
    var lang = 'en'; //English by default
    var match = re.exec(href);
    if (match !== null) {
        lang = match[1];
    }
    // initialize lightBox
    $('.object').lightBox({
        txtOf: languageMap[lang].txtOf,
        txtImage: languageMap[lang].txtImage
    });
})

有关实例,请参阅http://jsfiddle.net/ArtBIT/JzPZP/

另外http://jsfiddle.net/ArtBIT/JzPZP/1会显示俄语版​​

答案 1 :(得分:0)

我修改了lightbox.js对自身及其选项的控制:

//(function () {
//    var $, Lightbox, LightboxOptions;
//    $ = jQuery;
    var Lightbox, LightboxOptions;
    LightboxOptions = (function () {
        [...]
    })();

    Lightbox = (function () {
        [...]
    })();
//    $(function () {
//        var lightbox, options;
//        options = new LightboxOptions;
//        return lightbox = new Lightbox(options);
//    });
//}).call(this);

另一方面,我必须在每个我想要图库的页面中调用一个小代码片段,而不是只在头部添加de库:

$(function () {
    var lightbox, options;
    options = new LightboxOptions;
    return lightbox = new Lightbox(options);
});

但是现在,我可以以编程方式更改灯箱选项,而无需进行额外更改: ASP.net:

$(function () {
    var lightbox, options;
    options = new LightboxOptions;
    options.labelImage = '<%=this.GetLocalResourceObject("Lightbox.options.labelImage").ToString() %>';
    options.labelOf = '<%=this.GetLocalResourceObject("Lightbox.options.labelOf").ToString() %>';
    return lightbox = new Lightbox(options);
});

PHP:

$(function () {
    var lightbox, options;
    options = new LightboxOptions;
    options.labelImage = '<?php echo $dictionary["Lightbox_options_labelOf"]; ?>';
    options.labelOf = '<?php echo $dictionary["Lightbox.options.labelOf"]; ?>';
    return lightbox = new Lightbox(options);
});