2

lets say im making a translate system, it replace all the words that is matched in the array key with the value of the matched array key lets say

translate =
  saya: "i",
  jalan: "walk",
  hutan: "forest",
  beli: "buy",
  ke: "to",
  apel: "apple",
};

string = "SaYa JaLan ke hutan beli Apel" // case insensitive

it should be

string = "I walk to forest buy apple" // the translated string

how can we do that in javascript? in a function so we can call it like

translated = translate(string) // returns the translated string

8
  • not a good idea to do this in JS, even worse idea to translate a sentence merely by replacing words. If you are otherwise looking to learn how to replace strings in JS, read about the built-in .replace() method at MDN Commented Jun 29, 2012 at 17:47
  • do you have any other suggestion? and why is not a good idea translate a sentence merely by replacing words in javascript? Commented Jun 29, 2012 at 17:52
  • First off, what are you really trying to do? a translating service or you just want to translate a parts of your site, or what? Commented Jun 29, 2012 at 17:55
  • 2
    a simple a translating service, for my tribe language in my small village Commented Jun 29, 2012 at 17:56
  • 2
    translating sentences can get complicated because you need to translate more than words sometimes. You might run in a situation where the words alone won't be able to represent what the sentence actually says. You have to worry about linking verbs/words, conjuring verbs and things like that which would have no exact word translation or words that can translate to more than one meaning. Therefore I'd advise against doing it in JS and instead have a server side solution and use JS to send text to be processed and retrieve them back once it's done Commented Jun 29, 2012 at 18:08

2 Answers 2

4
function translate(txt) {
    var translate = {
        saya: "i",
        jalan: "walk",
        hutan: "forest",
        beli: "buy",
        ke: "to",
        apel: "apple"
    };

    for (var key in translate) {
        txt = txt.replace(new RegExp(key, "ig"), translate[key]);
    }
    return txt.charAt(0).toUpperCase() + txt.slice(1);  //Capitalize the first
}                                                          // letter.

var translated = translate("SaYa JaLan ke hutan beli Apel");
//"I walk to forest buy apple" 

Demo: http://jsfiddle.net/DerekL/2ucEk/

Sign up to request clarification or add additional context in comments.

3 Comments

what is ig? can u explain more?
@user1489281 - ig means g:Replace every word that matches, i:case insesitive
i means case insensitive (it won't matter if words are upper or lower case); g means global, so it will check the whole string and not just the first occurrence (which is the default behavior in js as opposed to C# or other languages)
-1

JS noobie myself but I think this should work. The words are different but the process is the same

    lexicon = new Object();
lexicon.merry = "god";
lexicon.christmas = "jul";
lexicon.and = "och";
lexicon.happy = "gott";
lexicon.new = "nytt";
lexicon.year = "ar";

function translate(){

    //use to show translation:
    //have a merry christmas and a fun new year!
    var myString = document.getElementById('input').value;

    myString = myString.replace(/merry/g, lexicon.merry);
    myString = myString.replace(/christmas/g, lexicon.christmas);
    myString = myString.replace(/and/g, lexicon.and);
    myString = myString.replace(/happy/g, lexicon.happy);
    myString = myString.replace(/new/g, lexicon.new);
    myString = myString.replace(/year/g, lexicon.year);

    document.write(myString);
}

3 Comments

new Object() is bad. Multiple separate replace is bad.
@Derek, I should use braces and define it explicitly? Also, I realize it was inefficient but didn't know how to do it in a for loop until I saw this so thanks for explaining
For more about looping through an object, see the MDN docs for...in. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.