0

I am new to jQuery. This code worked for me to change text in html:

// Some variables for later
var dictionary, set_lang;

// Object literal behaving as multi-dictionary
dictionary = {
    "english": {
        "_hello": "Hello",
        "_january": "January"
    },
    "portuguese": {
        "_hello": "Oie",
        "_january": "Janeiro"
    },
    "russian": {
        "_hello": "привет",
        "_january": "январь"
    }
};

$(function () {

    // Lets be professional, shall we?
    "use strict";

    // Function for swapping dictionaries
    set_lang = function (dictionary) {
        $("[data-translate]").text(function () {
            var key = $(this).data("translate");
            if (dictionary.hasOwnProperty(key)) {
                return dictionary[key];
            }
        });
    };

    // Swap languages when menu changes
    $("#lang").on("change", function () {
        var language = $(this).val().toLowerCase();
        if (dictionary.hasOwnProperty(language)) {
            set_lang(dictionary[language]);
        }
    });

    // Set initial language to English
    set_lang(dictionary.english);

});​

1- I would like to improve this same code to change some images too (src), how can I do it?

2- is it possible to simplify this code? It´s very difficult to a newbie understand this kind of code: functions within functions... ugh !!! (have a lot to study)

Thanks in advance.

P.S. This is the link to the original post: jQuery language switcher

I believe that something like this would do the trick:

// Some variables for later
var dictionary, set_lang;

// Object literal behaving as multi-dictionary
dictionary = {
    "english": {
        "_hello": "Hello",
        "_january": "January"
        "_imgSrc": "myStuff/img/imageEn.png" //added as a example to image change
    },
    "portuguese": {
        "_hello": "Oie",
        "_january": "Janeiro"
        "_imgSrc": "myStuff/img/imagePt.png" //added as a example to image change
    },
    "russian": {
        "_hello": "привет",
        "_january": "январь"
        "_imgSrc": "myStuff/img/imageRu.png" //added as a example to image change
    }
};

$(function () {

    // Lets be professional, shall we?
    "use strict";

    // Function for swapping text
    set_lang = function (dictionary) {
        $("[data-translate]").text(function () {
            var key = $(this).data("translate");
            if (dictionary.hasOwnProperty(key)) {
                return dictionary[key];
            }
        });
    };

    // Function for swapping images
        set_lang = function (dictionary) {
            $("[src-translate]").attr("src") === function () {  //here is the problem, how to implement this?
                var key = $(this).data("translate");
                if (dictionary.hasOwnProperty(key)) {
                    return dictionary[key];
                }
            });
        };

    // Swap languages when menu changes
    $("#lang").on("change", function () {
        var language = $(this).val().toLowerCase();
        if (dictionary.hasOwnProperty(language)) {
            set_lang(dictionary[language]);
        }
    });

    // Set initial language to English
    set_lang(dictionary.english);
Community
  • 1
  • 1
4cruzeta
  • 1
  • 2
  • 1
    JavaScript is not the same as Java! – TheValyreanGroup Nov 01 '16 at 22:50
  • 1- You'd have to think about a consistent file naming system for translated images, in order to change a part of the `src` of it. **Yes, possible** 2- Sorry, functions inside functions is a powerfull aspect of jQuery and many programming languages. Asking to avoid it is like asking for a *knitted knotless*. ;) This said, show an HTML sample of an image to change... – Louys Patrice Bessette Nov 01 '16 at 23:33
  • - Thanks for you effort @TheValyreanGroup, but I don't know how it can help me... – 4cruzeta Nov 02 '16 at 12:44
  • - Thanks, @Louys Patrice Bessette, I will try to improve the question to give you this information. – 4cruzeta Nov 02 '16 at 12:44
  • I though it would be possible to implement this peace of code in it: http://stackoverflow.com/a/554289/5859611 – 4cruzeta Nov 02 '16 at 13:33
  • With *«consistent file naming system»*, I mean to determine something in the image file name **or** in the image path to be able to find the right one using the image `src` attribute. Something like (for filename): `logo-english-.jpg` **or** (for path): `images/english/logo.jpg`. This has to be "consistent over all the image that can be replaced by a script, based on the language possibilities. – Louys Patrice Bessette Nov 02 '16 at 19:44

1 Answers1

0

I found a way to make it work as I wanted:

$(function () {

    // Lets be professional, shall we?
    "use strict";

    // Some variables for later
    var dictionary, set_lang;

    // Object literal behaving as multi-dictionary
    dictionary = {
        "english": {
            "_hello": "Hello",
            "_january": "January"
        },
        "portuguese": {
            "_hello": "Oie",
            "_january": "Janeiro"
        },
        "spanish": {
            "_hello": "¡Hola",
            "_january": "Enero"
        }
    };

    // Function for swapping dictionaries
    set_lang = function (dictionary) {
        $("[data-translate]").text(function () {
            var key = $(this).data("translate");
            if (dictionary.hasOwnProperty(key)) {
                return dictionary[key];
            }
        });
    };

    // Swap languages when menu changes
    $("#lang").on("change", function () {
        var language = $(this).val().toLowerCase();
           if (language == "portuguese") {
        $("#Bot").attr("src", "my_stuff/img/LogoPt.png").attr("alt", "Portuguese")
     
        } else
            if (language == "english") {
        $("#Bot").attr("src", "my_stuff/img/LogoEn.png").attr("alt", "English")
        
        } else
            if (language == "spanish") {
        $("#Bot").attr("src", "my_stuff/img/LogoEs.png").attr("alt", "Spanish")
       
        }
        if (dictionary.hasOwnProperty(language)) {
            set_lang(dictionary[language]);
        }
    });

    // Set initial language to English
    set_lang(dictionary.english);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="lang">
    <option>English</option>
    <option>Portuguese</option>
    <option>Spanish</option>
</select>
<span data-translate="_hello">Hello</span>, 
<span data-translate="_january">January</span>!
<br>
<br>
<img id="Bot" src="my_stuff/img/Logo.png" class="img-circle img-responsive" alt="english">

Thanks for your effort friends.

4cruzeta
  • 1
  • 2