balazs-endresz / jquery-translate

Automatically exported from code.google.com/p/jquery-translate
5 stars 5 forks source link

How I can manually correct some translations? #61

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
How I can manually correct some translations?
I am trying to get the data with the following code but I cannot...

alert($(this).data('translation')[lang][".$val"]);

How can I change(replace) some translated words with the ones I want?
Thanks.

Original issue reported on code.google.com by kostasvgt@gmail.com on 15 Feb 2011 at 5:00

GoogleCodeExporter commented 9 years ago
There's an extra dot in your code, correctly:
alert($(this).data('translation')[lang]["$val"]);

This is how you can manipulate the stored translation data:

//creating the objects if they don't yet exist:
if(!$(this).data('translation'))
  $(this).data('translation', {});
if(!$(this).data('translation')[lang])
  $(this).data('translation')[lang] = {};

//replace translation:
$(this).data('translation')[lang]["$val"] = "custom translation";

//or use the internal _getType function to determine the type of the element,
//which can be either "$html", "$val", or "someAttribute"
$(this).data('translation')[lang][ $.translate._getType(this, translateOptions) 
] = "custom translation";

But now we have only changed the data, which will only affect _further_ 
translations to the specific language, which is probably not exactly what you 
want, so I'd need some further details about the situation before I continue.

Original comment by balazs.endresz on 15 Feb 2011 at 8:14

GoogleCodeExporter commented 9 years ago
Hi,
thanks for your answer...Specifically, I have one function which translates the 
entire page and after the translation (in the complete function) I want to 
correct some wrong translated text...what I 've done so far is the following:

function TranslateTo(lang) {
    $(document).ready(function() {
        $("#DivContainer").translate(lang, {
            data: true,
            not: ".language",
            start: function() { $("#translating").show() },
            complete: function() {
                $("#translating").hide();
                alert($(this).data('translation')[lang]["$val"]);
                if (lang == "en") {
                    $("#DivContainer *").replaceText("Boat", "Skafos");
                }
            },
            error: function() { $("#translating").hide() }
        })
    });
    // set a cookie to remember the selected language
    $.cookie('destLang', lang);
}

How can I correct the translated data?The line
alert($(this).data('translation')[lang]["$val"]);
gives me error(it cannot recognise the data variable)...

Original comment by kostasvgt@gmail.com on 15 Feb 2011 at 10:23

GoogleCodeExporter commented 9 years ago
The `this` keyword inside callback functions refers to the translation object, 
not to the actual DOM element (also, that would make sense only in case of the 
`each` callback): 
http://code.google.com/p/jquery-translate/wiki/TranslateMethod#Callback_function
s
So you can modify the data and change the content as follows, assumung you know 
that you'd like to change form input elements only (hence "$val"):

        $("#DivContainer").translate(lang, {
            data: true,
            not: ".language",
            start: function() { $("#translating").show() },
            each: function(i, e, transl, src, from, to) {
               if((to == "en") && transl.match("Boat")){
                 var newTransl = transl.replace("Boat", "Skafos");
                 $(e).val(newTransl)
                     .data('translation')[to].$val = newTransl;
               }
            },
            complete: function() {
                $("#translating").hide();
            },
            error: function() { $("#translating").hide() }
        })

Original comment by balazs.endresz on 16 Feb 2011 at 2:36

GoogleCodeExporter commented 9 years ago
Thanks my friend,
this worked perfectly...

Original comment by kostasvgt@gmail.com on 16 Feb 2011 at 2:53

GoogleCodeExporter commented 9 years ago
Great!

Original comment by balazs.endresz on 16 Feb 2011 at 2:57

GoogleCodeExporter commented 9 years ago
Sorry now it does not work...

function TranslateTo(lang) {
    $(document).ready(function() {
        $("#DivContainer").translate(lang, {
            data: true,
            not: ".language",
            start: function() { $("#translating").show() },
            each: function(i, e, transl, src, from, to) {
                if ((to == "en") && transl.match("Boat")) {
                    var newTransl = transl.replace("Boat", "Skafos");
                    $(e).val(newTransl).data('translation')[to].$val =
newTransl;
                }
            },
            complete: function() { $("#translating").hide(); },
            error: function() { $("#translating").hide() }
        })
    });
    // set a cookie to remember the selected language
    $.cookie('destLang', lang);
}

Original comment by kostasvgt@gmail.com on 16 Feb 2011 at 3:00

GoogleCodeExporter commented 9 years ago
Actually, it finds the "Boat" word but cannot replace it...

Original comment by kostasvgt@gmail.com on 16 Feb 2011 at 3:09

GoogleCodeExporter commented 9 years ago
Finally this is what I ve done to change the contents of my translated
element:

function TranslateTo(lang) {
    $(document).ready(function() {
        $("#DivContainer").translate(lang, {
            data: true,
            not: ".language",
            start: function() { $("#translating").show() },
            each: function(i, e, transl, src, from, to) {
                if ((to == "en") && transl.match("Boat")) {
                    var newTransl = transl.replace("Boat", "Skafos");
                    $(e).context.innerHTML=newTransl;
                }
            },
            complete: function() { $("#translating").hide(); },
            error: function() { $("#translating").hide() }
        })
    });
    // set a cookie to remember the selected language
    $.cookie('destLang', lang);
}

2011/2/16 Kostas Tsimirikas <kostasvgt@gmail.com>

Original comment by kostasvgt@gmail.com on 16 Feb 2011 at 3:49

GoogleCodeExporter commented 9 years ago
As I mentioned before my solution applies for input elements only - I suspected 
that because you used "$val" before in your code. Otherwise you have to use 
"$html" to modify the data, though it's probably unnecessary in your case.

Original comment by balazs.endresz on 17 Feb 2011 at 10:47

GoogleCodeExporter commented 9 years ago
Hi,
yes actually I didn't understand what is the difference between $val and $html.
However the line

$(e).context.innerHTML=newTransl;

did the job...

Original comment by kostasvgt@gmail.com on 17 Feb 2011 at 11:04