开源软件名称(OpenSource Name):m1/go-localize开源软件地址(OpenSource Url):https://github.com/m1/go-localize开源编程语言(OpenSource Language):Go 98.4%开源软件介绍(OpenSource Introduction):go-localizeSimple 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. UsageGo generateThe suggested way to use go-localize is to use
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:
Example of TOML translation file: hello = "hello"
how_are_you = "How are you?" To then generate the localization package, add the following to your //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
{
"hello": "hello customer!"
} You'll be able to access this using the key: SuggestionsIt is suggested to instead of using hardcoded locale keys i.e. ReplacementsTake this replacement string for example: hello_firstname_lastname: Hello {{.firstname}} {{.lastname}} To then replace 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 fallbacksYou can define the locale and fallbacks using: l := localizations.New("en", "es") Where println(l.Get("key_doesnt_exist")) //"key_doesnt_exist" will be printed Translation file supportWe currently support JSON and YAML translation files. Please suggest missing file type using issues or pull requests. CLIInstead of using go generate you can just generate the localizations manually using
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论