Repository

v translator

v module to translate string or word (uses google, mymemory and linguee)

Install

v install Saverio976.translator

Example

translate a word

import saverio976.translator

fn main(){
    english_word := 'hello'
    italian_word := translator.translate_word('en', 'it', english_word)
    println('en : ${english_word}')
    println('it : ${italian_word}')
}

translate sentence

import saverio976.translator

fn main(){
    english_text := 'hello world !'
    french_text := translator.translate_text('en', 'fr', english_text)
    println('en : ${english_text}')
    println('fr : ${french_text}')
}

use the translator.ContextTranslator to choose which online translator you want

import saverio976.translator

fn main(){
    mut context := translator.ContextTranslator{
        src_language: 'en'
        dest_language: 'fr'
        src_text: 'hello world'
    }
    // using google
    if context.google_translate() != 0 {
        println('the text is not translated')
    }
    else {
        println('${context.src_language} : ${context.src_text}')
        println('${context.dest_language} : ${context.dest_text}')
    }
    // using mymemory
    if context.mymemory_translate() != 0 {
        println('the text is not translated')
    }
    else {
        println('${context.src_language} : ${context.src_text}')
        println('${context.dest_language} : ${context.dest_text}')
    }
    // using linguee
    context = translator.ContextTranslator{
        src_language: 'en'
        dest_language: 'it'
        src_text: 'hello' 		// linguee translate only word
                                // if there is more than one word, it will use 
                                // internaly another method (mymemory , google)
    }
    if context.linguee_translate() != 0 {
        println('the word is not translated')
    }
    else {
        println('${context.src_language} : ${context.src_text}')
        println('${context.dest_language} : ${context.dest_text}')
    }
}

module

Contents

translate_text

fn translate_text(src_lng string, dest_lng string, text string) string

translate_x_to_y create a ContextTranslator with src_lng & dest_lng & text and return translated string if the translation update the ContextTranslator.dest_text, else return an empty string

[Return to contents]

translate_word

fn translate_word(src_lng string, dest_lng string, word string) string

translate_word use linguee and get the first result, return this result else, try to using mymemory to translate, then google, and result an empty string if can't get any response https://www.linguee.com/{source}-{target}/translation/{word}.html

[Return to contents]

ContextTranslator

struct ContextTranslator {
pub:
    src_language  string
    dest_language string
    src_text      string
pub mut:
    dest_text string
}

ContextTranslator used for function and the method translate

[Return to contents]

mymemory_translate

fn (mut c ContextTranslator) mymemory_translate() int

translate with mymemory. return 0 if ContextTranslator.dest_text is updated, else return -1 https://api.mymemory.translated.net/get?q={text}&langpair={source}|{target}

[Return to contents]

google_translate

fn (mut c ContextTranslator) google_translate() int

translate with google. return 0 if ContextTranslator.dest_text is updated, else return -1 'https://translate.googleapis.com/translate_a/single?client=gtx&sl={source}&tl={target}&dt=t&q={text}'

[Return to contents]

linguee_translate

fn (mut c ContextTranslator) linguee_translate() int

translate one word with linguee. return 0 if ContextTranslator.dest_text is updated, else return -1 https://www.linguee.com/{source}-{target}/translation/{word}.html

[Return to contents]

Powered by vdoc. Generated on: 3 Jun 2021 19:51:01

tree

"."
├── "assets"
│   ├── "cli-translate-example.v"
│   └── "DEFAULT_README.md"
├── "html_entity"
│   ├── "generate"
│   │   ├── "entities.json"
│   │   ├── "gen_entitytable.v"
│   │   └── "README.md"
│   ├── "html_entity_test.v"
│   └── "html_entity.v"
├── "context_translator.v"
├── "language.v"
├── "LICENSE"
├── "Makefile"
├── "README.md"
├── "translator_test.v"
├── "translator.v"
├── "url_endpoint.v"
└── "v.mod"

3 directories, 16 files

About

v module to translate string using https://translate.google.com, https://mymemory.translated.net or https://www.linguee.com/

0
0
last May 16

Author

Saverio976