8000 GitHub - go-i18n/i18n: Package i18n provides internationalization and localization for your Go applications · GitHub
[go: up one dir, main page]

Skip to content
go-i18nPublic

About

Package i18n provides internationalization and localization for your Go applications

Topics

Resources

Contributing

Stars

51 stars

Watchers

2 watching

Forks

Repository files navigation

i18n

GitHub Workflow Status codecov GoDoc Sourcegraph

Package i18n provides internationalization and localization for your Go applications.

Installation

The minimum requirement of Go is 1.27.

go get unknwon.dev/i18n

Getting started

# locale_en-US.ini
[plurals]
file.one = file
file.other = files

dog.one = dog
dog.other = dogs

[messages]
test1 = This patch has %[1]d changed ${file, 1} and deleted %[2]d ${file, 2}
test2 = I have %[1]d ${dog, 1}
# locale_zh-CN.ini
[plurals]
file.other = 文件

[messages]
test1 = 该补丁变更了 %[1]d 个${file, 1}并删除了 %[2]d 个${file, 2}
package main

import (
	"fmt"

	"unknwon.dev/i18n"
)

func main() {
	s := i18n.NewStore()
	l1, err := s.AddLocale("en-US", "English", "locale_en-US.ini")
	// ... handler error

	l2, err := s.AddLocale("zh-CN", "简体中文", "locale_zh-CN.ini")
	// ... handler error

	fmt.Println(l1.Translate("messages::test1", 1, 2))
	// => "This patch has 1 changed file and deleted 2 files"

	fmt.Println(l2.Translate("messages::test1", 1, 2))
	// => "该补丁变更了 1 个文件并删除了 2 个文件"

	fmt.Println(l2.TranslateWithFallback(l1, "messages::test2", 1))
	// => "I have 1 dog"
}

Named placeholders

Use TranslateNamed (or TranslateNamedWithFallback) with name/value pairs to fill {name} placeholders by name. A name may contain ASCII letters, digits and underscores. The message is not formatted by fmt.Sprintf, so literal % characters are kept as-is. A placeholder without a matching argument is left unchanged, and a value is never substituted again even if it contains another placeholder.

A plural can take its count from a named argument as well, e.g. ${file, count}.

[plurals]
file.one = file
file.other = files

[messages]
test3 = Hello, {name}!
test4 = {user} changed {count} ${file, count}
fmt.Println(l1.TranslateNamed("messages::test3", "name", "Joe"))
// => "Hello, Joe!"

fmt.Println(l1.TranslateNamed("messages::test4", "user", "Joe", "count", 2))
// => "Joe changed 2 files"

License

This project is under the MIT License. See the LICENSE file for the full license text.

About

Package i18n provides internationalization and localization for your Go applications

Topics

Resources

Contributing

Stars

51 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages

0