Coder Social home page Coder Social logo

kataras / i18n Goto Github PK

View Code? Open in Web Editor NEW
109.0 4.0 12.0 130 KB

:new: High-performant and powerful localization and internationalization support for Go

Home Page: https://en.wikipedia.org/wiki/Website_localization

License: MIT License

Go 100.00%
i18n golang go middleware yaml toml json translate localization iris

i18n's Introduction

i18n (Go)

build status report card godocs donate on Stripe

Efficient and easy to use localization and internationalization support for Go.

Installation

The only requirement is the Go Programming Language.

$ go get github.com/kataras/i18n@latest

Examples

Getting started

Create a folder named ./locales and put some YAML, TOML, JSON or INI files.

│   main.go
└───locales
    ├───el-GR
    │       example.yml
    ├───en-US
    │       example.yml
    └───zh-CN
            example.yml

Now, put the key-values content for each locale, e.g. ./locales/en-US/example.yml

hi: "Hi %s"
#
# Templates are supported
# hi: "Hi {{ .Name }}
#
# Template functions are supported
# hi: "Hi {{sayHi .Name}}
# ./locales/el-GR/example.yaml
hi: "Γειά σου %s"
# ./locales/zh-CN/example.yaml
hi: 您好 %s

Some other possible filename formats...

  • page.en.yaml
  • home.cart.el-GR.json
  • /el/file.tml

The language code MUST be right before the file extension.

The Default I18n instance will try to load locale files from ./locales directory. Use the Tr package-level function to translate a text based on the given language code. Use the GetMessage function to translate a text based on the incoming http.Request. Use the Router function to wrap an http.Handler (i.e an http.ServeMux) to set the language based on path prefix such as /zh-CN/some-path and subdomains such as zh.domain.com without the requirement of different routes per language.

Let's take a look at the simplest usage of this package.

package main

import (
	"fmt"

	"github.com/kataras/i18n"
)

type user struct {
	Name string
	Age  int
}

func main() {
	// i18n.SetDefaultLanguage("en-US")

	// Fmt style.
	enText := i18n.Tr("en", "hi", "John Doe") // or "en-US"
	elText := i18n.Tr("el", "hi", "John Doe")
	zhText := i18n.Tr("zh", "hi", "John Doe")

	fmt.Println(enText)
	fmt.Println(elText)
	fmt.Println(zhText)

	// Templates style.
	templateData := user{
		Name: "John Doe",
		Age:  66,
	}

	enText = i18n.Tr("en-US", "intro", templateData) // or "en"
	elText = i18n.Tr("el-GR", "intro", templateData)
	zhText = i18n.Tr("zh-CN", "intro", templateData)

	fmt.Println(enText)
	fmt.Println(elText)
	fmt.Println(zhText)
}

Load specific languages over a new I18n instance. The default language is the first registered, in that case is the "en-US".

I18n, err := i18n.New(i18n.Glob("./locales/*/*"), "en-US", "el-GR", "zh-CN")

Load embedded files through a go-bindata package:

I18n, err := i18n.New(i18n.Assets(AssetNames, Asset), "en-US", "el-GR", "zh-CN")

Load embedded files through Go's embed directive (recommended):

//go:embed static/locales/*
var staticFS embed.FS

loader, err := i18n.FS(staticFS, "./static/locales/*/*.yml")
// [handle error...]
I18n, err := i18n.New(loader, "en-US", "el-GR", "zh-CN")

Load through a simple Go map:

m := i18n.LangMap{
    "en-US": i18n.Map{
        "buy":               `buy %d`,
        "cart.checkout":     `checkout - {{.Param}}`,
        "cart.after.thanks": `thanks`,
        //
        "JSONTemplateExample":  `value of {{.Value}}`,
        "TypeOf":               `type of %T`,
        "KeyOnlyOnDefaultLang": `value`,
        //
        "title": `Title`,
        "hi":    `Hi {{.Name}}`,
        "int":   `1`,
        "hello": `Hello %s`,
        //
        "welcome": `welcome`,
    },
    "el-GR": i18n.Map{
        "buy":               `αγοράστε %d`,
        "cart.checkout":     `ολοκλήρωση παραγγελίας - {{.Param}}`,
        "cart.after.thanks": `ευχαριστούμε`,
        //
        "JSONTemplateExample": `τιμή του {{.Value}}`,
        "TypeOf":              `τύπος %T`,
        //
        "title": `Τίτλος`,
        "hi":    `Γειά σου {{.Name}}`,
        "int":   `1`,
        //
        "welcome": `καλώς ήρθατε`,
    },
}

loader := i18n.KV(m)

i18N, err := i18n.New(loader, "en-US", "el-GR")

Template variables & functions

Using template variables & functions as values in your locale value entry via LoaderConfig.

We are going to use a 3rd-party package for plural and singular words. Note that this is only for english dictionary, but you can use the "current" Locale and make a map with dictionaries to pluralize words based on the given language.

Before we get started, install the necessary packages:

$ go get github.com/kataras/i18n@master
$ go get github.com/gertd/go-pluralize@master

Let's create two simple translation files for our example. The ./locales/en-US/welcome.yml and ./locales/el-GR/welcome.yml respectfully:

Dog: "dog"
HiDogs: Hi {{plural (tr "Dog") .count }}
Dog: "σκυλί"
HiDogs: Γειά {{plural (tr "Dog") .count }}

The tr template function is a builtin function registered per locale. It returns the key's translated value. E.g. on english file the tr "Dog" returns the Dog:'s value: "dog" and on greek file it returns "σκυλί". This function helps importing a key to another key to complete a sentence.

Now, create a main.go file and store the following contents:

package main

import (
    "fmt"
    "text/template"

    "github.com/kataras/i18n"
    "github.com/gertd/go-pluralize"
)

var pluralizeClient = pluralize.NewClient()

func getFuncs(current *i18n.Locale) template.FuncMap {
    return template.FuncMap{
        "plural": func(word string, count int) string {
            return pluralizeClient.Pluralize(word, count, true)
        },
    }
}

func main() {
    I18n, err := i18n.New(i18n.Glob("./locales/*/*", i18n.LoaderConfig{
        // Set custom functions per locale!
        Funcs: getFuncs,
    }), "en-US", "el-GR", "zh-CN")
    if err != nil {
        panic(err)
    }

    textEnglish := I18n.Tr("en", "HiDogs", map[string]interface{}{
        "count": 2,
    }) // prints "Hi 2 dogs".
    fmt.Println(textEnglish)

    textEnglishSingular := I18n.Tr("en", "HiDogs", map[string]interface{}{
        "count": 1,
    }) // prints "Hi 1 dog".
    fmt.Println(textEnglishSingular)

    textGreek := I18n.Tr("el", "HiDogs", map[string]interface{}{
        "count": 1,
    }) // prints "Γειά 1 σκυλί".
    fmt.Println(textGreek)
}

Use go run main.go to run our small Go program. The output should look like this:

Hi 2 dogs
Hi 1 dog
Γειά 1 σκυλί

HTTP

HTTP, automatically searches for url parameter, cookie, custom function and headers for the current user language.

mux := http.NewServeMux()

I18n.URLParameter = "lang" // i.e https://domain.com?lang=el
I18n.Cookie = "lang"
I18n.ExtractFunc = func(r *http.Request) string { /* custom logic */ }

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    translated := I18n.GetMessage(r, "hi", "John Doe")
    fmt.Fprintf(w, "Text: %s", translated)
})

Prefer GetLocale if more than one GetMessage call.

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    locale := I18n.GetLocale(r)
    translated := locale.GetMessage("hi", "John Doe")
    // [...some locale.GetMessage calls]
})

Optionally, identify the current language by subdomain or path prefix, e.g. en.domain.com and domain.com/en or domain.com/en-US and e.t.c.

I18n.Subdomain = true

http.ListenAndServe(":8080", I18n.Router(mux))

If the ContextKey field is not empty then the Router will set the current language.

I18n.ContextKey = "lang" 

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    currentLang := r.Context().Value("lang").(string)
    fmt.Fprintf(w, "Language: %s", currentLang)
})

Set the translate function as a key on a HTML Template.

templates, _ := template.ParseGlob("./templates/*.html")

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    // per-request.
    translateFunc := I18n.GetLocale(r).GetMessage

    templates.ExecuteTemplate(w, "index.html", map[string]interface{}{
        "tr": translateFunc,
    })

    // {{ call .tr "hi" "John Doe" }}
})

Global function with the language as its first input argument.

translateLangFunc := I18n.Tr
templates.Funcs(template.FuncMap{
    "tr": translateLangFunc,
})

// {{ tr "en" "hi" "John Doe" }}

For a more detailed technical documentation you can head over to our godocs. And for executable code you can always visit the _examples repository's subdirectory.

License

kataras/i18n is free and open-source software licensed under the MIT License.

i18n's People

Contributors

dependabot[bot] avatar kataras avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

i18n's Issues

Example using Go Embed.FS

Thanks for this great library, I put together an example of how to use it with Go 1.16 embed.FS:

package locales

import (
	"embed"
)

//go:embed */*.yml
var translations embed.FS

func I18n() (*i18n.I18n, error) {
	// assuming embedded files at ./en/*.yml
	return i18n.New(i18n.Assets(translationNames, translations.ReadFile), "en")
}

func translationNames() (filenames []string) {
	fs.WalkDir(translations, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		if d.IsDir() {
			return nil
		}

		filenames = append(filenames, path)
		return nil
	})

	return filenames
}

[BUG] Language chosen as "ga" when AcceptLang only has English

Describe the bug

Automatic language detection recently got broken in my project.

To Reproduce
Steps to reproduce the behavior:

  1. Browser sends Accept-Language: en-GB,en;q=0.5
  2. i18n chooses to display ga

Expected behavior
Since both en-GB and en are supplied, and en is available as a folder (and not missing any translations), en should be chosen in my eyes.
ga is missing some strings, so English is used for them instead.

Desktop (please complete the following information):

  • OS: Debian GNU/Linux 11
  • Go version: go1.19.3

Additional context
Add any other context about the problem here.

file structure of locales
locales/
locales/nl
locales/nl/base.yaml
locales/da
locales/da/base.yaml
locales/en
locales/en/other-component.yaml
locales/en/base.yaml
locales/ga
locales/ga/base.yaml
locales/pl
locales/pl/base.yaml
locales/fr
locales/fr/base.yaml
locales/de
locales/de/other-component.yaml
locales/de/base.yaml
Further notes
  • I cannot find any other indicators for languages. There is no "lang" cookie. There is no request GET parameter. Hostname is "localhost".
  • code excerpt (registering API method, I did no further interaction with your library AFAICS)
    locale := i18n.GetLocale(r)
    templateData["tr"] = locale.GetMessage

It works in another browser where I send Accept-Language: de,en-US;q=0.7,en;q=0.3 (de is chosen / displayed).

Appreciate your help :) Thank you!

Upgrade dependencies

It would be nice to upgrade dependencies of the project, because I think it is starting to pull some old versions of other packages.

Example of upgraded code is in my fork commit: 080c239

[BUG] can't read yaml

Describe the bug

a:
  b:
    - 1
    - 2

Tr("en","a") -> ""
expect

b: 
  - 1
  - 2

To Reproduce
Steps to reproduce the behavior:

  1. [...]

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. ubuntu, windows]

Additional context
Add any other context about the problem here.

[BUG] es-ES: unexpected type of []interface {} as value

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. [...]

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. ubuntu, windows]

Additional context
Add any other context about the problem here.

Issue when using string in translated text

Describe the bug
I'm trying a basic implementation using english and spanish translations files but when trying to do this:

func main() {
	enText := i18n.Tr("en", "textWithLink", "A123456") 

	fmt.Println(enText)
}

and in my locales/en-US I have my example json like:

{ "textWithLink": "Lorem ipsum [This is my link](https://domain.example.com/form/%s/create)" }

Then I'm getting on my result translation: Lorem ipsum [This is my link](https://domain.example.com/form/[A123456]/create)

string passed as parameter is printing between [ ]

Why is happening this?

[BUG] Inconsistent language matching when loaded as a map

Describe the bug

(*I18n) Tr() is not consistent when producing translated templates. It seems the index of selecting/searching the language is not correct. I use custom language tags e.g. ka-KE (Kamba language).

To Reproduce

Steps to reproduce the behavior:

https://goplay.tools/snippet/55lBTIz199v

Running it multiple times shows that it randomly displays either fr-FR or ka-KE.

Expected behavior

Should only display ka-KE consistently.

Screenshots
N/A

Desktop (please complete the following information):

  • go version go1.20.4 linux/amd64

[BUG] Can't use i18n.SetDefaultLanguage("en-US")

Describe the bug
When using i18n.SetDefaultLanguage("en-US") in the init() function, an error is threw at runtime:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x8d204e]

goroutine 1 [running]:
github.com/kataras/i18n.(*I18n).SetDefault(0x0, {0xa9b1d5?, 0x587?})
	/home/titi/go/pkg/mod/github.com/kataras/[email protected]/i18n.go:173 +0x4e
github.com/kataras/i18n.SetDefaultLanguage(...)
	/home/titi/go/pkg/mod/github.com/kataras/[email protected]/i18n.go:43
main.init.0()
	/home/titi/Dev/api/telegram/friendly-brocolli/main.go:44 +0xa6
exit status 2

To Reproduce
Steps to reproduce the behavior:

  1. Make an init() function
  2. Put i18n.SetDefaultLanguage("en-US") inside

Expected behavior
Shouldn't throw an error but continue the code

Screenshots
image

Desktop (please complete the following information):

  • OS: Debian 12
  • Go: go1.19.8 linux/amd64

Additional context
Add any other context about the problem here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.