开发者

Reading language from source

I would like to use lightbox in multi-language (en, ru, lt) site, so i ne开发者_开发百科ed to translate lightbox. To set correct parameters to lightbox i need to read language witch is defined in:

    <body>
        <div id="main">

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

So if i'm in another language logo link would be that language code. I have this .js code:

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

How i should alter JS to do the checking?


$(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
    });
})

See http://jsfiddle.net/ArtBIT/JzPZP/ for a live example.

Also http://jsfiddle.net/ArtBIT/JzPZP/1 will show you russian version


I modified lightbox.js the get control of itself and its options:

//(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);

On the other hand I have to call a little code snippet in every page I want the gallery instead of just adding de library in the head:

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

But now, I can change programatically the lightbox options without doing extra changes: 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);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜