Initial commit
This commit is contained in:
42
example/example.go
Normal file
42
example/example.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/text/language"
|
||||
"golang.org/x/text/language/display"
|
||||
|
||||
i18n "github.com/derfenix/goi18n"
|
||||
)
|
||||
|
||||
//go:embed locales/**/*.json
|
||||
var locales embed.FS
|
||||
|
||||
func Basic() {
|
||||
if err := i18n.Init(locales); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
printer := i18n.GetPrinter(language.Russian)
|
||||
fmt.Println(printer.Sprintf("test", "теста"))
|
||||
|
||||
printer = i18n.GetPrinter(language.English)
|
||||
fmt.Println(printer.Sprintf("test", "beer"))
|
||||
|
||||
ctx := i18n.ContextWithLang(context.Background(), language.Russian)
|
||||
handler(ctx)
|
||||
|
||||
ctx = i18n.ContextWithLang(context.Background(), language.English)
|
||||
handler(ctx)
|
||||
}
|
||||
|
||||
func handler(ctx context.Context) {
|
||||
translated := i18n.Sprintf(ctx, "test plural", 2)
|
||||
fmt.Println(translated)
|
||||
|
||||
lang := i18n.LanguageFromContext(ctx)
|
||||
script, _ := lang.Script()
|
||||
fmt.Println("Использован", display.Languages(language.Russian).Name(lang), "язык,", display.Scripts(language.Russian).Name(script))
|
||||
}
|
||||
17
example/example_test.go
Normal file
17
example/example_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package example_test
|
||||
|
||||
import (
|
||||
"github.com/derfenix/goi18n/example"
|
||||
)
|
||||
|
||||
func ExampleBasic() {
|
||||
example.Basic()
|
||||
|
||||
// Output:
|
||||
// Тест теста
|
||||
// Test of the beer
|
||||
// всего пара пауков
|
||||
// Использован русский язык, кириллица
|
||||
// just pair of spiders
|
||||
// Использован английский язык, латиница
|
||||
}
|
||||
38
example/locales/en/active.json
Normal file
38
example/locales/en/active.json
Normal file
@@ -0,0 +1,38 @@
|
||||
[
|
||||
{
|
||||
"key": "test",
|
||||
"description": "Для тестов, не трогать",
|
||||
"translation": "Test of the %s"
|
||||
},
|
||||
{
|
||||
"key": "test plural",
|
||||
"description": "Для тестов, не трогать",
|
||||
"plural": {
|
||||
"other": "exactly %d spiders",
|
||||
"one": "spider",
|
||||
"=0": "no spiders",
|
||||
"=2": "just pair of spiders"
|
||||
}
|
||||
},
|
||||
{
|
||||
"key": "transition_not_allowed",
|
||||
"description": "Ошибка при попытке произвести запрещённое изменение состояния",
|
||||
"translation": "Transition from '%s' to '%s' not allowed"
|
||||
},
|
||||
{
|
||||
"key": "Should be shorter than %d symbols",
|
||||
"translation": "Should be shorter than %d symbols"
|
||||
},
|
||||
{
|
||||
"key": "Should be longer than %d symbols",
|
||||
"translation": "Should be longer than %d symbols"
|
||||
},
|
||||
{
|
||||
"key": "Deleted",
|
||||
"translation": "Deleted"
|
||||
},
|
||||
{
|
||||
"key": "Published",
|
||||
"translation": "Published"
|
||||
}
|
||||
]
|
||||
38
example/locales/ru/active.json
Normal file
38
example/locales/ru/active.json
Normal file
@@ -0,0 +1,38 @@
|
||||
[
|
||||
{
|
||||
"key": "test",
|
||||
"description": "Для тестов, не трогать",
|
||||
"translation": "Тест %s"
|
||||
},
|
||||
{
|
||||
"key": "test plural",
|
||||
"description": "Для тестов, не трогать",
|
||||
"plural": {
|
||||
"other": "всего %d пауков",
|
||||
"one": "паучок",
|
||||
"=0": "нет пауков",
|
||||
"=2": "всего пара пауков"
|
||||
}
|
||||
},
|
||||
{
|
||||
"key": "transition_not_allowed",
|
||||
"description": "Ошибка при попытке произвести запрещённое изменение состояния",
|
||||
"translation": "Переход от '%s' к '%s' запрещён"
|
||||
},
|
||||
{
|
||||
"key": "Should be shorter than %d symbols",
|
||||
"translation": "Должно быть короче %d символов"
|
||||
},
|
||||
{
|
||||
"key": "Should be longer than %d symbols",
|
||||
"translation": "Должно быть длиннее %d символов"
|
||||
},
|
||||
{
|
||||
"key": "Deleted",
|
||||
"translation": "Удалён"
|
||||
},
|
||||
{
|
||||
"key": "Published",
|
||||
"translation": "Опубликован"
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user