mateussouzaweb / translator

Translation extraction from any code to JSON files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Translator - Translation extraction from any code to JSON files

Translator extracts all translatable terms of your project and places everything into a single JSON file that can be read from almost any programing language - including JavaScript.

It was originally designed to use in conjunction with multi-language SPA projects but is extensible to any kind of project that you are developing, whatever the source code language - if can reads JSON, will works.


Features

  • Written in Go Language and provided as package module
  • Extracts terms from __() and translate() functions
  • Supports plurals from __p() and pluralize() functions
  • Supports terms extraction from custom patterns
  • Reads terms from the following files by default: .html, .js, .jsx, .ts .tsx, .mjs, .mts and .go
  • You can include any other file extension to extract translations
  • Keeps already translated terms on the destination file
  • Remove old terms that are not used anymore
  • Can be use to extract and translate Go projects on the fly
  • Just works!

CLI - Installation and Usage

To install, just download the most recent binary file:

REPOSITORY="https://github.com/mateussouzaweb/translator/releases/download/latest"
sudo wget $REPOSITORY/translator -O /usr/local/bin/translator
sudo chmod +x /usr/local/bin/translator

To check command flags use:

translator --help

To translate a project, run:

translator \
    --source /path/to/source/ \
    --destination /path/to/destination/json/file.json

Now translate the terms by editing the JSON file. Enjoy!


Usage In Go Projects

Start by requiring the go module:

go get github.com/mateussouzaweb/translator

Load the translation file and start using it:

package main

import (
    "fmt"
    "log"
    "github.com/mateussouzaweb/translator/i18n"
)

translator := i18n.Context{
    Code:  "pt-BR",
    Alias: "pt",
    File:  "translations/pt-BR.json",
}

err := translator.Load()

if err != nil {
    log.Fatal(err)
}

fmt.Println( translator.Translate("Hello World!") )
// Result: Olá Mundo!

Enjoy!

About

Translation extraction from any code to JSON files

License:MIT License


Languages

Language:Go 97.3%Language:Makefile 2.7%