Skip to content

translate text from one language to another on node.js and the browser. 30+ languages supported, simple as cake.

Notifications You must be signed in to change notification settings

Marak/translate.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

translate.js - translates text to different languages, simple as cake

v0.6.0

USAGE

node.js -

    var sys = require('sys');
    var colors = require('colors'); // colors are fun!
    var translate = require('./lib/translate');

    // note: the translator is  English=>Spanish by default
    translate.text('I want tacos please.', function(err, text){

      sys.puts('I want tacos please.'.red + ' => '.cyan + text.yellow);
      var input = 'Spanish', output = "Japanese";
      translate.text({input:input,output:output}, text, function(err, text2){

        var input = 'Japanese', output = "English";
        sys.puts(text.yellow + ' => '.cyan + text2.blue);
        translate.text({input:input,output:output}, text, function(err, text3){

           sys.puts(text2.blue + ' => '.cyan + text3.red);
           sys.puts('English'.red+'=>'+'Spanish'.yellow+'=>'+'Japanese'.blue+'=>'+'English'.red  +'\ntaco request has been normalized. ^_^'.green);
        });
      }); 
    });

browser -

      <!-- main translate.js library -->
      <script type="text/javascript" src="./lib/translate.js"></script>

      <!-- load up the languages definition file -->
      <script type="text/javascript" src="./lib/languages.js"></script>

      <!-- jquery not required, just used in the demo page -->
      <script type="text/javascript" src="./vendor/jquery.js"></script>

      <script type="text/javascript">
        var languages;

        $(document).ready(function(){

          languages = getLangs();

          // populate the select box
          for(var lang in languages){
            $('#langInput').append('<option value = "'+lang+'">' + lang + '</option>');
          }

          for(var lang in languages){
            $('#langOutput').append('<option value = "'+lang+'">' + lang + '</option>');
          }

          /***** NAMED EVENTS *****/

            $(document).bind('##TRANSLATE_TEXT##', function(e){

              $('#run').attr('disabled','disabled');
              $('#run').val('translating...');     
              var input = $('#langInput').val(), output = $('#langOutput').val();

              translate.text({input:input,output:output}, $('#theCode').val(), function(result){
                $('#run').attr('disabled','');
                $('#run').val('<== translate ==>');
                $('#output').val( result );
              });
            });

          /**** END NAMED EVENTS ****/

          /**** BIND UI EVENTS ****/

            // select box change
            $('#langSelector').change(function(){
              $(document).trigger('##CHANGE_LANGUAGE##', {"fontName":$(this).val()})
            });

            $('#run').click(function(e){
              $(document).trigger('##TRANSLATE_TEXT##');
            });

          /**** END UI BIND EVENTS ****/

          // little bit of a onReady hack. i'll fix the API a bit so this can be done better
          $(document).trigger('##CHANGE_LANGUAGE##', {"fontName":'Doh'});
          $('#langInput').val('English');
          $('#langOutput').val('Spanish');

        });
      </script>

languages

Afrikaans
Albanian
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bulgarian
Catalan
Chinese
Croatian
Czech
Danish
Dutch
English
Estonian
Filipino
Finnish
French
Galician
Georgian
German
Greek
Haitian Creole
Hebrew
Hindi
Hungarian
Icelandic
Indonesian
Irish
Italian
Japanese
Korean
Latvian
Lithuanian
Macedonian
Malay
Maltese
Norwegian
Persian
Polish
Portuguese
Romanian
Russian
Serbian
Slovak
Slovenian
Spanish
Swahili
Swedish
Thai
Turkish
Ukrainian
Urdu
Vietnamese
Welsh
Yiddish

fun facts

translate.js uses the google api and requires an internet connection
if you want to actually hear the translated text as audio you could use say.js

Authors

Andrew Lunny (alunny), Marak Squires, Google

About

translate text from one language to another on node.js and the browser. 30+ languages supported, simple as cake.

Resources

Stars

Watchers

Forks

Packages

No packages published