Goによる国際化とローカライズ
19603 ワード
ハロー・ギャル.
この記事では、単一言語のWebアプリケーションを複数言語に変換する方法を学びます.
本条で使用する道具
go-bindata (オプション)
myapp
, Irisをインストールする$ go mod init myapp
$ go get github.com/kataras/iris/v12@master
以下のチュートリアルに従ってください.国際化とローカライズ
ローカライズ機能は、簡単にアプリケーション内の複数の言語をサポートできるように、さまざまな言語で文字列を取得する便利な方法を提供します.言語文字列は./locales
ディレクトリ.このディレクトリの中には、アプリケーションがサポートする各言語のサブディレクトリがあります.
│ main.go
└───locales
├───el-GR
│ home.yml
├───en-US
│ home.yml
└───zh-CN
home.yml
アプリケーションの既定の言語は、最初の登録言語です.
app := iris.New()
// First parameter: Glob filpath patern,
// Second variadic parameter: Optional language tags,
// the first one is the default/fallback one.
app.I18n.Load("./locales/*/*", "en-US", "el-GR", "zh-CN")
または、
app.I18n.Load("./locales/*/*")
// Then set the default language using:
app.I18n.SetDefault("en-US")
組み込みロケールを読み込む
アプリケーションの実行可能な範囲内で、「go - bindata」ツールを使用してロケールを埋め込むことができます.
│ main.go
└───locales
├───el-GR
│ home.yml
├───en-US
│ home.yml
└───zh-CN
home.yml
app := iris.New()
// First parameter: Glob filpath patern,
// Second variadic parameter: Optional language tags,
// the first one is the default/fallback one.
app.I18n.Load("./locales/*/*", "en-US", "el-GR", "zh-CN")
app.I18n.Load("./locales/*/*")
// Then set the default language using:
app.I18n.SetDefault("en-US")
$ go get -u github.com/go-bindata/go-bindata/...
$ go-bindata -o locales.go ./locales/...
LoadAssets
言語の初期化と読み込みの方法AssetNames
and Asset
関数はgo-bindata
ap.I18n.LoadAssets(AssetNames, Asset, "en-US", "el-GR", "zh-CN")
翻訳の定義
ロカールファイルはYAML(推奨)、JSON、TOMLまたはINI形式で記述できます.
それぞれのファイルはキーを含んでいるべきです.キーはサブキーを持つことができます.
それぞれのキーの値はフォーム
string
or map
翻訳されたテキスト(またはテンプレート)またはその多値キー値を含む.アイリスI 18 Nモジュールは、ボックスから複数の機能をサポートしています.
FMTスタイル
hi: "Hi %s!"
ctx.Tr("Hi", "John")
// Outputs: Hi John!
テンプレート
hi: "Hi {{.Name}}!"
ctx.Tr("Hi", iris.Map{"Name": "John"})
// Outputs: Hi John!
多元化
アイリスI 18 Nは複数の変数をサポートします.ロケール変数を定義するには
新しいセクションを定義する
Vars
キー.変数のキーは以下の通りです.
one
"=x"
ここでxは番号"<x"
other
format
Vars:
- Minutes:
one: "minute"
other: "minutes"
- Houses:
one: "house"
other: "houses"
次に、各メッセージはこの変数を使用できます.# Using variables in raw string
YouLate: "You are %[1]d ${Minutes} late."
# [x] is the argument position,
# variables always have priority other fmt-style arguments,
# that's why we see [1] for houses and [2] for the string argument.
HouseCount: "%[2]s has %[1]d ${Houses}."
ctx.Tr("YouLate", 1)
// Outputs: You are 1 minute late.
ctx.Tr("YouLate", 10)
// Outputs: You are 10 minutes late.
ctx.Tr("HouseCount", 2, "John")
// Outputs: John has 2 houses.
あなたが与えられた複数のカウントに基づいて表示されるメッセージを選択することができます.変数を除いて、各々のメッセージも、その複数形を持つこともできます!
許容キー
zero
one
two
"=x"
"<x"
">x"
other
FreeDay:
"=3": "You have three days and %[2]d ${Minutes} off." # "FreeDay" 3,15
one: "You have a day off." # "FreeDay", 1
other: "You have %[1]d free days." # "FreeDay", 5
ctx.Tr("FreeDay", 3, 15)
// Outputs: You have three days and 15 minutes off.
ctx.Tr("FreeDay", 1)
// Outputs: You have a day off.
ctx.Tr("FreeDay", 5)
// Outputs: You have 5 free days.
テンプレートテキスト+関数+複数+変数を使用して、もう少し高度な例を続けましょう.Vars:
- Houses:
one: "house"
other: "houses"
- Gender:
"=1": "She"
"=2": "He"
VarTemplatePlural:
one: "${Gender} is awesome!"
other: "other (${Gender}) has %[3]d ${Houses}."
"=5": "{{call .InlineJoin .Names}} are awesome."
const (
female = iota + 1
male
)
ctx.Tr("VarTemplatePlural", iris.Map{
"PluralCount": 5,
"Names": []string{"John", "Peter"},
"InlineJoin": func(arr []string) string {
return strings.Join(arr, ", ")
},
})
// Outputs: John, Peter are awesome
ctx.Tr("VarTemplatePlural", 1, female)
// Outputs: She is awesome!
ctx.Tr("VarTemplatePlural", 2, female, 5)
// Outputs: other (She) has 5 houses.
セクション
キーが予約されていない場合(例: 1 , 2 ...)その後、サブセクションとして機能します.セクションはドット文字で区切られます(
.
).Welcome:
Message: "Welcome {{.Name}}"
ctx.Tr("Welcome.Message", iris.Map{"Name": "John"})
// Outputs: Welcome John
現在のロケールの決定
あなたは
context.GetLocale
判定方法現在のロケールか、ロケールが指定した値かどうかを調べます.
func(ctx iris.Context) {
locale := ctx.GetLocale()
// [...]
}
ロケールインターフェイスはこのようになります.// Locale is the interface which returns from a
// `Localizer.GetLocale` metod.
// It serves the transltions based on "key" or format. See `GetMessage`.
type Locale interface {
// Index returns the current locale index from the languages list.
Index() int
// Tag returns the full language Tag attached tothis Locale,
// it should be uniue across different Locales.
Tag() *language.Tag
// Language should return the exact languagecode of this `Locale`
//that the user provided on `New` function.
//
// Same as `Tag().String()` but it's static.
Language() string
// GetMessage should return translated text based n the given "key".
GetMessage(key string, args ...interface{}) string
}
翻訳の取得
利用
context.Tr
このリクエストの翻訳テキストを取得するショートカット.func(ctx iris.Context) {
text := ctx.Tr("hi", "name")
// [...]
}
インサイドビュー
func(ctx iris.Context) {
ctx.View("index.html", iris.Map{
"tr": ctx.Tr,
})
}
読書ありがとう🙏 楽しいし、屋内で安全にする💪Follow my GitHub and .
Special thanks to
Reference
この問題について(Goによる国際化とローカライズ), 我々は、より多くの情報をここで見つけました https://dev.to/kataras/internationalization-and-localization-with-go-3b56テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol