Translate your Go program into multiple languages with similar fmt.Sprintf format syntax.

Loafer-i18n

Loafer-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

  • Supports pluralized strings with =x or >x expression.
  • Supports strings with similar to fmt.Sprintf format syntax.
  • Supports message files of any format (e.g. JSON, TOML, YAML).

Package i18n

The i18n package provides support for looking up messages according to a set of locale preferences.

package main

import (
	"fmt"
	"os"

	_ "github.com/EasyGram-io/loafer-i18n/example/catalog"
	"github.com/EasyGram-io/loafer-i18n/i18n"
	"golang.org/x/text/language"
)

func main() {

	i18n.SetLang(language.SimplifiedChinese)

	i18n.Printf(`hello world!`)
	fmt.Println()

	name := `EasyGram.io`

	i18n.Printf(`hello %s!`, name)
	fmt.Println()

	i18n.Printf(`%s has %d cat.`, name, 1)
	fmt.Println()

	i18n.Printf(`%s has %d cat.`, name, 2, i18n.Plural(
		`%[2]d=1`, `%s has %d cat.`,
		`%[2]d>1`, `%s has %d cats.`,
	))
	fmt.Println()

	i18n.Fprintf(os.Stderr, `%s have %d apple.`, name, 2, i18n.Plural(
		`%[2]d=1`, `%s have an apple.`,
		`%[2]d=2`, `%s have two apples.`,
		`%[2]d>2`, `%s have %d apples.`,
	))
	fmt.Println()
}

Command loaferi18n

The loaferi18n command manages message files used by the i18n package.

go get -u github.com/EasyGram-io/loafer-i18n/loaferi18n
loaferi18n -h

  update, u    merge translations and generate catalog
  extract, e   extracts strings to be translated from code
  generate, g  generates code to insert translated messages

Extracting messages

Use loaferi18n extract . ./locales/en.json to extract all i18n.Sprintf function literals in Go source files to a message file for translation.

./locales/en.json

{
  "%s has %d cat.": "%s has %d cat.",
  "%s has %d cats.": "%s has %d cats.",
  "%s have %d apples.": "%s have %d apples.",
  "%s have an apple.": "%s have an apple.",
  "%s have two apples.": "%s have two apples.",
  "hello %s!": "hello %s!",
  "hello world!": "hello world!"
}

Translating a new language

  1. Create an empty message file for the language that you want to add (e.g. zh-Hans.json).

  2. Run loaferi18n update ./locales/en.json ./locales/zh-Hans.json to populate zh-Hans.json with the mesages to be translated.

    ./locales/zh-Hans.json

    {
      "%s has %d cat.": "%s有%d只猫。",
      "%s has %d cats.": "%s有%d只猫。",
      "%s have %d apples.": "%s有%d个苹果。",
      "%s have an apple.": "%s有一个苹果。",
      "%s have two apples.": "%s有两个苹果。",
      "hello %s!": "你好%s!",
      "hello world!": "你好世界!"
    }
  3. After zh-Hans.json has been translated, run loaferi18n generate --pkg=catalog ./locales ./catalog/catalog.go.

  4. Import catalog package in main.go, example: import _ "github.com/EasyGram-io/loafer-i18n/example/catalog"

Translating new messages

If you have added new messages to your program:

  1. Run loaferi18n extract to update ./locales/en.json with the new messages.
  2. Run loaferi18n update ./locales/en.json to generate updated ./locales/new-language.json files.
  3. Translate all the messages in the ./locales/new-language.json files.
  4. Run loaferi18n generate --pkg=catalog ./locales ./catalog/catalog.go to merge the translated messages into the go files.

For examples:

Thanks:

License

Loafer-i18n is available under the MIT license. See the LICENSE file for more info.

Owner
EasyGram
Make it easier for you to use Telegram!
EasyGram
Similar Resources

Using NFP (Number Format Parser) you can get an Abstract Syntax Tree (AST) from Excel number format expression

NFP (Number Format Parser) Using NFP (Number Format Parser) you can get an Abstract Syntax Tree (AST) from Excel number format expression. Installatio

Feb 4, 2022

Translate Prometheus Alerts into Kubernetes pod readiness

prometheus-alert-readiness Translates firing Prometheus alerts into a Kubernetes readiness path. Why? By running this container in a singleton deploym

Oct 31, 2022

Cli tool to translate text from any language into german

GERMAN A cli tool for converting text into German. Build Locally $ go build $ go install Dependencies To execute successfully, a free tier DEEPL API

Jan 24, 2022

Golang ultimate ANSI-colors that supports Printf/Sprintf methods

Golang ultimate ANSI-colors that supports Printf/Sprintf methods

Aurora Ultimate ANSI colors for Golang. The package supports Printf/Sprintf etc. TOC Installation Usage Simple Printf aurora.Sprintf Enable/Disable co

Dec 29, 2022

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows.

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows.

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染

Dec 30, 2022

Toy scripting language with a syntax similar to Rust.

Dust - toy scripting language Toy scripting language with a syntax similar to Rust. 👍 Syntax similar to Rust 👍 Loose JSON parsing 👍 Calling host fu

Sep 28, 2022

:art: Contextual fmt inspired by bootstrap color classes

Cfmt Contextual fmt It provides contextual formatting functions that have nearly identical usage of the fmt package. The ideas were borrowed from boot

Jan 7, 2023

Same as fmt.Errorf but with stack trace.

Annotation with stack trace for go1.13 Go 1.13 contains support for error wrapping. Now you can add additional information to an error by wrapping it

Dec 25, 2020

A lightweight replacement for the standard fmt package, reduces binary size by roughly 400kb in a hello world

console This is a lightweight replacement for the fmt package, reduces the binary size by roughly 400kb in a hello world program. Please note: This pa

Nov 7, 2021

Linter for Go's fmt.Errorf message

wrapmsg wrapmsg is Go code linter. this enforces fmt.Errorf's message when you wrap error. Example // OK 👍🏻 if err := pkg.Cause(); err != nil { re

Dec 27, 2022

📖 Tutorial: An easy way to translate your Golang application

📖 Tutorial: An easy way to translate your Golang application

📖 Tutorial: An easy way to translate your Golang application 👉 The full article is published on April 13, 2021, on Dev.to: https://dev.to/koddr/an-e

Feb 9, 2022

Our notification system simplifies the process of sending notifications via email, SMS, and push notifications for multiple applications. It supports multiple providers, customizable templates, and is easy to integrate into any application.

Our notification system simplifies the process of sending notifications via email, SMS, and push notifications for multiple applications. It supports multiple providers, customizable templates, and is easy to integrate into any application.

Simplify Notification Management with Customizable Templates and Multi-Provider Integration ⭐️ Why Envoyer Nowadays, notifications play a crucial role

May 11, 2023

URL-friendly slugify with multiple languages support.

slug Package slug generate slug from unicode string, URL-friendly slugify with multiple languages support. Documentation online Example package main

Jan 4, 2023

Stalin sort in multiple languages!

stalin-sort Stalin sort in multiple languages, contributions are welcome! Motivation This repo is motivated by this tweet, this tweet contains a refer

Jan 14, 2022

Snippit - Creates syntax-highlighted code snippets in png or svg format

Snippit - Creates syntax-highlighted code snippets in png or svg format

snippit creates syntax-highlighted code snippets in png or svg format. Installat

Oct 10, 2022

Jazigo is a tool written in Go for retrieving configuration for multiple devices, similar to rancid, fetchconfig, oxidized, Sweet.

Table of Contents About Jazigo Supported Platforms Features Requirements Quick Start - Short version Quick Start - Detailed version Global Settings Im

Jan 5, 2023

Split multiple Kubernetes files into smaller files with ease. Split multi-YAML files into individual files.

Split multiple Kubernetes files into smaller files with ease. Split multi-YAML files into individual files.

Dec 29, 2022
📖 Tutorial: An easy way to translate your Golang application
📖 Tutorial: An easy way to translate your Golang application

?? Tutorial: An easy way to translate your Golang application ?? The full article is published on April 13, 2021, on Dev.to: https://dev.to/koddr/an-e

Feb 9, 2022
Complete Translation - translate a document to another language
 Complete Translation - translate a document to another language

Complete Translation This project is to translate a document to another language. The initial target is English to Korean. Consider this project is no

Feb 25, 2022
[UNMANTEINED] Extract values from strings and fill your structs with nlp.

nlp nlp is a general purpose any-lang Natural Language Processor that parses the data inside a text and returns a filled model Supported types int in

Nov 24, 2022
Package i18n provides internationalization and localization for your Go applications.

i18n Package i18n provides internationalization and localization for your Go applications. Installation The minimum requirement of Go is 1.16. go get

Nov 9, 2022
i18n-pseudo - Pseudolocalization is an incredibly useful tool for localizing your apps.

i18n-pseudo Pseudolocalization is an incredibly useful tool for localizing your apps. This module makes it easy to apply pseudo to any given string. I

Mar 21, 2022
Translate your Go program into multiple languages.

go-i18n go-i18n is a Go package and a command that helps you translate Go programs into multiple languages. Supports pluralized strings for all 200+ l

Jan 1, 2023
Tool that can parse Go files into an abstract syntax tree and translate it to several programming languages.
Tool that can parse Go files into an abstract syntax tree and translate it to several programming languages.

GoDMT GoDMT, the one and only Go Data Model Translator. The goal of this project is to provide a tool that can parse Go files that include var, const,

Nov 28, 2022
go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

Jan 2, 2023
Show Languages In Code. A fast and lightweight CLI to generate stats on the languages inside your project
Show Languages In Code. A fast and lightweight CLI to generate stats on the languages inside your project

slic Show Languages In Code. Usage Run it with an -h flag to list all commands. -d flag can be used to specify the directory of search -i flag can be

Dec 25, 2021