i18n (Internationalization and localization) engine written in Go, used for translating locale strings.

go-localize

GoDoc Build Status Go Report Card Release codecov

Simple and easy to use i18n (Internationalization and localization) engine written in Go, used for translating locale strings. Use with go generate or on the CLI. Currently supports JSON, YAML, TOML and CSV translation files

Why another i18n library?

This package aims to be as simple and easy to use as possible. It also takes inspiration from popular localization libraries/packages in other languages - so makes it easier to reason about coming from other languages and frameworks.

Usage

Go generate

The suggested way to use go-localize is to use go generate. For example, take the following directory structure:

goapp
└── localizations_src
    ├── en
    │   └── messages.yaml
    └── es
        ├── customer
        │   └── messages.json
        └── messages.json

Example of JSON translation file:

{
  "hello": "Hola",
  "how_are_you": "¿Cómo estás?",
  "whats_your_name": "¿Cuál es tu nombre?",
  "hello_my_name_is": "Hola, mi nombre es {{.name}}"
}

Example of YAML translation file:

hello: hello
how_are_you: How are you?
whats_your_name: "What's your name?"
hello_my_name_is: Hello my name is {{.name}}
hello_firstname_lastname: Hello {{.firstname}} {{.lastname}}

Example of CSV translation file:

hello, hello
how_are_you, How are you?

Example of TOML translation file:

hello = "hello"
how_are_you = "How are you?"

To then generate the localization package, add the following to your main.go or another one of your .go files:

//go:generate go-localize -input localizations_src -output localizations

Now you'll be able to use the localization like so:

l := localizations.New("en", "es")

println(l.Get("messages.how_are_you")) // How are you?

println(l.GetWithLocale("es", "messages.hello_my_name_is", &localizations.Replacements{"name":"steve"})) // "Hola, mi nombre es steve"

With en being the locale and es being the fallback. The localization keys are worked out using folder structure, eg:

en/customer/messages.json with the contents being:

{
  "hello": "hello customer!"
}

You'll be able to access this using the key: customer.messages.hello.

Suggestions

It is suggested to instead of using hardcoded locale keys i.e. en to use the language keys included in key, i.e: language.BritishEnglish.String() which is en-GB

Replacements

Take this replacement string for example:

hello_firstname_lastname: Hello {{.firstname}} {{.lastname}}

To then replace firstname and the lastname variable, you can use something like this:

l := localizations.New("en", "es")
println(l.Get("hello_firstname_lastname", &localizations.Replacements{"firstname": "steve", "lastname": "steve"}))

You can also append numerous replacements if you have them like so:

println(l.Get("hello_firstname_lastname", &localizations.Replacements{"firstname": "steve"}, &localizations.Replacements{"lastname": "steve"}))

Locale defining and localization fallbacks

You can define the locale and fallbacks using:

l := localizations.New("en", "es")

Where en is the locale and es is the fallback. If no translation key-value is found then the key will be returned. For example

println(l.Get("key_doesnt_exist")) //"key_doesnt_exist" will be printed

Translation file support

We currently support JSON and YAML translation files. Please suggest missing file type using issues or pull requests.

CLI

Instead of using go generate you can just generate the localizations manually using go-localize:

Usage of go-localize:
  -input string
        input localizations folder
  -output string
        where to output the generated package
Owner
Miles Croxford
Senior Software Engineer
Miles Croxford
Similar Resources

A Golang library for text processing, including tokenization, part-of-speech tagging, and named-entity extraction.

prose is a natural language processing library (English only, at the moment) in pure Go. It supports tokenization, segmentation, part-of-speech tagging, and named-entity extraction.

Jan 4, 2023

Self-contained Machine Learning and Natural Language Processing library in Go

If you like the project, please ★ star this repository to show your support! 🤩 A Machine Learning library written in pure Go designed to support rele

Dec 30, 2022

Stemmer packages for Go programming language. Includes English, German and Dutch stemmers.

Stemmer package for Go Stemmer package provides an interface for stemmers and includes English, German and Dutch stemmers as sub-packages: porter2 sub

Dec 14, 2022

A Go package for n-gram based text categorization, with support for utf-8 and raw text

A Go package for n-gram based text categorization, with support for utf-8 and raw text. To do: write documentation make it faster Keywords: text categ

Nov 28, 2022

A go library for reading and creating ISO9660 images

iso9660 A package for reading and creating ISO9660 Joliet and Rock Ridge extensions are not supported. Examples Extracting an ISO package main import

Jan 2, 2023

Gopher-translator - A HTTP API that accepts english word or sentences and translates them to Gopher language

Gopher Translator Service An interview assignment project. To see the full assig

Jan 25, 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

Package i18n is a middleware that provides internationalization and localization for Flamego

Package i18n is a middleware that provides internationalization and localization for Flamego

i18n Package i18n is a middleware that provides internationalization and localization for Flamego. Installation The minimum requirement of Go is 1.16.

Dec 14, 2022

Go-i18n - i18n for Golang

I18n for Go Installation go get -u github.com/fitv/go-i18n Usage YAML files ├──

Oct 18, 2022

app-services-go-linter plugin analyze source tree of Go files and validates the availability of i18n strings in *.toml files

app-services-go-linter app-services-go-linter plugin analyze source tree of Go files and validates the availability of i18n strings in *.toml files. A

Nov 29, 2021

2D triangulation library. Allows translating lines and polygons (both based on points) to the language of GPUs.

2D triangulation library. Allows translating lines and polygons (both based on points) to the language of GPUs.

triangolatte 2D triangulation library. Allows translating lines and polygons (both based on points) to the language of GPUs. Features normal and miter

Dec 23, 2022

A program to create assembly 8086 strings to print without using any printing/strings related function but only mov-xchg-int and loops

Assembly String builder tool A program to create assembly 8086 strings to print without using any printing/strings related function but only mov-xchg-

Feb 1, 2022

CxGo is a tool for translating C source code to Go

C to Go translator CxGo is a tool for translating C source code to Go (aka transpiler, source-to-source compiler). It uses cc for preprocessing and pa

Jan 1, 2023

Cross platform locale detection for Golang

go-locale go-locale is a Golang lib for cross platform locale detection. OS Support Support all OS that Golang supported, except android: aix: IBM AIX

Aug 20, 2022

Cross platform locale detection for Golang

go-locale go-locale is a Golang lib for cross platform locale detection. OS Support Support all OS that Golang supported, except android: aix: IBM AIX

Aug 20, 2022

This is a POC for a Falco Plugin allowing to gather events from a locale docker daemon.

Docker Events Plugin This is a POC for a Falco Plugin allowing to gather events from a locale docker daemon. ⚠️ This is a POC, don't use in Production

Apr 15, 2022

Fast face detection, pupil/eyes localization and facial landmark points detection library in pure Go.

Fast face detection, pupil/eyes localization and facial landmark points detection library in pure Go.

Pigo is a pure Go face detection, pupil/eyes localization and facial landmark points detection library based on Pixel Intensity Comparison-based Objec

Dec 24, 2022

Localizer: convenient localization for Go

Localizer: convenient localization for Go Localizer intends to make it easy for you to work with locales in Go. It was inspired by many good tools tha

Nov 8, 2022

linenoise-classic is a command-line tool that generates strings of random characters that can be used as reasonably secure passwords.

linenoise-classic is a command-line tool that generates strings of random characters that can be used as reasonably secure passwords.

Aug 21, 2022
Comments
  • Make output gofmt-compliant

    Make output gofmt-compliant

    This PR fixes some stray whitespace in the template (and thus generated code). Also, map values are now lined up, so now the code fully complies the Go code style and passes gofmt without any changes.

  • Output path flexibility issue

    Output path flexibility issue

    Dear contributors,

    I experienced technical difficulty in generating the localization file in a specified path that suits my project folder structure. I was generating the file using CLI as shown below: Want:

    go-localize -input ./features/i18n/data/dataSources/language -output ./core/i18n/go-localize/generated
    

    Got:

    2021/08/18 16:55:47 open ./core/i18n/go-localize/generated/./core/i18n/go-localize/generated.go: The system cannot find the path specified.
    

    Furthermore, the version that is readily available on pkg.go.dev is not the recent version as shown in this repo version, which is 0.3.0.

    Hope to hear from you soon.

Go-i18n - i18n for Golang

I18n for Go Installation go get -u github.com/fitv/go-i18n Usage YAML files ├──

Oct 18, 2022
Cross platform locale detection for Golang

go-locale go-locale is a Golang lib for cross platform locale detection. OS Support Support all OS that Golang supported, except android: aix: IBM AIX

Aug 20, 2022
i18n of golang

i18n i18n of golang 使用方法 下载i18n go get https://github.com/itmisx/i18n 定义 code 语言包 var langPack1 = map[string]map[interface{}]interface{}{ "zh-cn": {

Dec 11, 2021
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
[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
Self-contained Japanese Morphological Analyzer written in pure Go
Self-contained Japanese Morphological Analyzer written in pure Go

Kagome v2 Kagome is an open source Japanese morphological analyzer written in pure golang. The dictionary/statistical models such as MeCab-IPADIC, Uni

Dec 24, 2022
Go efficient text segmentation and NLP; support english, chinese, japanese and other. Go 语言高性能分词

gse Go efficient text segmentation; support english, chinese, japanese and other. 简体中文 Dictionary with double array trie (Double-Array Trie) to achiev

Jan 8, 2023
Utilities for working with discrete probability distributions and other tools useful for doing NLP work

GNLP A few structures for doing NLP analysis / experiments. Basics counter.Counter A map-like data structure for representing discrete probability dis

Nov 28, 2022
Read and use word2vec vectors in Go

Introduction This is a package for reading word2vec vectors in Go and finding similar words and analogies. Installation This package can be installed

Nov 28, 2022
Selected Machine Learning algorithms for natural language processing and semantic analysis in Golang

Natural Language Processing Implementations of selected machine learning algorithms for natural language processing in golang. The primary focus for t

Dec 25, 2022