tinoni / translate.js

A jQuery plugin to translate text in the client side
MIT License
87 stars 84 forks source link

Not sure to understand #16

Open cyberlp23 opened 5 years ago

cyberlp23 commented 5 years ago

Hello, Sorry, i'm not sure to understand how this works. Should I only add something like this? It doesn't seem to work. Well, it does recognize the right language defined in $('body').translate({lang: "en", t: dict}); , but nothing happens when clicking on the translation buttons. Also, do I need in the var dict to put the whole text of the variable, or can I use for example the id of an element? Thanks!

    <p class="trn">to be translated</p>

           <a class="btn btn-default lang_selector trn" href="#" role="button" data-value="en">EN</a>
            <a class="btn btn-default lang_selector trn" href="#" role="button"  data-value="fr">FR</a>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="jquery.translate.js"/>
    <script type="text/javascript">
        var dict = {
            "to be translated": {
                en: "the translation",
fr: "la traduction"
            }
        }
        $('body').translate({lang: "en", t: dict}); 
    </script>
mohammedFurqan commented 5 years ago

@cyberlp23 Here is a working JSFiddle for you.

I notice 3 issues in your above code:

  1. Your dict object should have the keys as strings, but you haven't written them as such. It should be enclosed within ".

  2. You have to instantiate the translate. After instantiating you should call the translate method.

var translator = $('body').translate({lang: "en", t: dict}); translator.lang("pt"); // Mention the language you want to translate to

  1. You need to add an event listener on the buttons to help translate. This you haven't added.

Check the above fiddle I have shared. It should be of help to you.