translate

package module
v2.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2022 License: BSD-2-Clause Imports: 16 Imported by: 1

README

translate

Tools to maintain meaning ordered word files.

It is a port of some pre go modules tools that go modules broke. It was also broken by the Google Translate API changes and API security changes.

It has one command line tool translate.

translate tool

translate is a tool for managing meaning ordered words files.

A meaning ordered words file is a file which has words, in one language, based on another file in a different language. The file name specifies the language. The filename must be of the form XX.words, where XX is a ISO-639 two letter language code.

For instance:

en.words Easy to use. Easy.

de.words Einfach zu gebrauchen. Einfach.

fi.words Helppokäyttöinen. Helppo.

It uses the Google Translate API V3 for translating.

Usage:

translate [-words wordsDir] [-credentials credentialsJson] command [arguments]

The commands are:

add mainLang newLang [newLang..]
  Add a new meaning ordered words file for newLang based on mainLang to
  wordsDir.

check
  Quick wordsDir check.  Does not check translation accuracy just
  consistency.  Does not call the Google Translate API.

supported displayLang
  Show the current Google supported languages in displayLang.

update mainLang
  Updates all meaning ordered words files in wordsDir.  Effectively,
  calls add on each existing non-mainLang language.

More on meaning ordered word files

A meaning ordered word file is a just a list of words and phrases. The file names take the form ISO639.words. Where ISO639 is an ISO-639 two letter language code.

A directory of these files can be maintained with these tools so that for different languages the meanings correspond by line.

Reference

Cloud Translation API

More Cloud Translation API

golang

github.com/napcatstudio/translate/v2

Documentation

Overview

bcp47 contains:

information correlating ISO 639 and ISO 3166 codes,
see http://www.rfc-editor.org/rfc/bcp/bcp47.txt

translate Commands for translating words and phrases using Google APIs and caching the results to minimize future API calls.

iso3266-2.go source: https://en.wikipedia.org/wiki/ISO_3166-2 on February 28, 2022 I has to combine the subdivisions by hand.

iso639-2.go from the Library of Congress Standards https://www.loc.gov/standards/iso639-2/ISO-639-2_8859-1.txt downloaded on February 28, 2022.

words provides information on what languages we have translations (words) for.

xlns.go Google Cloud API translate functions for words files.

Common translation tools.

Index

Constants

View Source
const (
	PROJECT_ID = "project_id"
)
View Source
const (
	RAW_ISO3166_2 = `` /* 9720-byte string literal not displayed */

)
View Source
const (
	RAW_ISO639_2 = `` /* 15592-byte string literal not displayed */

)
View Source
const WORDS_SUFFIX = ".words"

Variables

This section is empty.

Functions

func CountryForIso3166

func CountryForIso3166(iso3166code string) string

CountryForIso3166 returns the country name for the given ISO-3166 code. Will return "" if the code is not present.

func CountryHasLanguage

func CountryHasLanguage(country, language string) bool

CountryHasLanguage tells whether a language is spoken in the given country.

func HasColonialLanguage

func HasColonialLanguage(country string) bool

HasLanguagesForCountry returns whether or not a country has English, French, German, Dutch, or Spanish as a language.

func HasLanguagesForCountry

func HasLanguagesForCountry(country string) bool

HasLanguagesForCountry tells whether we have language information for the country.

func IsCamelCase

func IsCamelCase(s string) bool

IsCamelCase return true if the string is camelCase.

func IsEs419

func IsEs419(country string) bool

IsEs419 returns true if the given country is part of Spanish speaking Latin America. This helps with BCP-47 code es-419.

func Iso3166Countries

func Iso3166Countries() []string

Iso3166Countries returns the list of ISO-3166 countries.

func Iso3166ForCountry

func Iso3166ForCountry(country string) string

Iso3166ForCountry returns the ISO-3166 code for a country. Will return "" if the country is not found.

func Iso3166FromBcp47

func Iso3166FromBcp47(bcp47 string) string

Iso3166FromBcp47 extracts the ISO 3166 country code from the BCP-47 code if it exists. Note that in BCP-47 there are special codes that are not ISO 3166.

func Iso639ForLanguage

func Iso639ForLanguage(language string) string

Iso639ForLanguage returns the ISO-639 code for a English name. Will return "" if the language is not found.

func Iso639FromBcp47

func Iso639FromBcp47(bcp47 string) string

Iso639FromBcp47 extracts the ISO 639 language code from the BCP-47 code.

func LanguageForCountry

func LanguageForCountry(country string) string

LanguageForCountry returns the primary language for a country. Returns "" if unknown.

func LanguageForIso639

func LanguageForIso639(iso639code string) string

LanguageForIso639 return the first applicable English language name for the given ISO 639 code (some languages have more than one name). Will return "" if the code is not present.

func WordsCheck

func WordsCheck(wordsDir string) error

WordsCheck does very simplistic verification that a wordsDir is consistent.

func WordsFilename

func WordsFilename(wordsDir, lang string) string

WordsFilename returns the path of the words file for the given language.

func WordsGetLang

func WordsGetLang(wordsDir, bcp47 string) (string, error)

WordsGetLang returns the words file language to use for locale 'bcp47'.

func WordsGetWords

func WordsGetWords(wordsDir, bcp47 string) ([]string, error)

WordsGetWords returns the words for the given language.

func WordsHasLanguage

func WordsHasLanguage(wordsDir, bcp47 string) bool

WordsHasLanguage returns whether or not we have a translation words file for the given code. This used to check for strict ISO-639 languages and not be case sensitive.

func WordsLanguages

func WordsLanguages(wordsDir string) ([]string, error)

WordsLanguages returns a list of ISO-639 or BCP-47 codes from the available words files.

func WordsLanguagesMap

func WordsLanguagesMap(wordsDir string) (map[string]bool, error)

WordsLanguagesMap returns a map of language to bool for the languages in wordsDir.

func WordsMerge

func WordsMerge(toWordsDir, fromWordsDir string) error

WordsMerge adds the words in fromWordsDir to toWordsDir. If toWordsDir has more languages than fromWordsDir it bails.

func WordsWriteWords

func WordsWriteWords(wordsDir, lang string, words []string) error

WordsWriteWords writes the words for a language.

func XlnsAdd

func XlnsAdd(wordsDir, credentialsJson, mainLang string, newLangs []string) error

XlnsAdd adds new languages to a meaning ordered words directory.

func XlnsSupported

func XlnsSupported(credentialsJson, lang string) error

XlnsSupported outputs the list of Google Cloud Translate API supported languages.

func XlnsUpdate

func XlnsUpdate(wordsDir, credentialsJson, mainLang string) error

XlnsUpdate updates all meaning ordered words files based by translating from mainLang. It retranslates the whole file.

Types

type Iso3166_2

type Iso3166_2 struct {
	Code, Country, Subdivisions string
}

func Iso3166_2s_all

func Iso3166_2s_all() []Iso3166_2

type Iso639_2

type Iso639_2 struct {
	Bibliographic, Terminologic, Code string
	English, French                   []string
}

func Iso639_2_all

func Iso639_2_all() []Iso639_2

Iso639_2_all returns all of the processed ISO 639-2 data.

func (Iso639_2) String

func (iso639 Iso639_2) String() string

type XlnsMap

type XlnsMap map[string]string

func NewXlnsMap

func NewXlnsMap(base1, base2 io.Reader) (XlnsMap, error)

TranslationMap takes two lists of words ordered by meaning and creates a map from the first to the second.

func WordsXlnsMap

func WordsXlnsMap(wordsDir, sourceLang, targetLang string) (XlnsMap, error)

WordsXlnsMap creates an XlnsMap object for the given languages.

func XlnsMapFromFiles

func XlnsMapFromFiles(w1, w2 string) (XlnsMap, error)

XlnsMapFromFiles creates a XlnsMap from the words files w1 and w2.

func (XlnsMap) Key

func (xm XlnsMap) Key() []string

Key returns the "key" words as a sorted slice.

func (XlnsMap) Translate

func (xm XlnsMap) Translate(source, target string) string

Translate a string using the translation map. Use target if there is no other tranlation.

func (XlnsMap) TranslateByLine

func (xm XlnsMap) TranslateByLine(source string) string

TranslateByLine translates source looking for entire lines. If that fails it will translate by word (see TranslateText).

func (XlnsMap) TranslateByLineWithAlternate

func (xm XlnsMap) TranslateByLineWithAlternate(source, altSource string, limit int) string

TranslateByLineWithAlternate translates like TranslateByLine but if the translation is too long translates alternative text instead.

func (XlnsMap) TranslateText

func (xm XlnsMap) TranslateText(text string) string

TranslateText by word.

func (XlnsMap) TranslateWords

func (xm XlnsMap) TranslateWords(words []string) []string

TranslateWords translates a list of words. If there is no translation for a word it will be skipped.

Directories

Path Synopsis
cmd
translate
translate updates meaning ordered words files.
translate updates meaning ordered words files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL