i18n

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2022 License: MIT Imports: 4 Imported by: 0

README

I18n for Go

Install

go get -u github.com/fitv/go-i18n

Usage

YAML files

├── locales
│   ├── en.yml
│   ├── zh.yml
└── main.go
package main

import "github.com/fitv/go-i18n"

//go:embed locales/*.yml
var fs embed.FS

func main() {
    i18n, err := i18n.New(fs, "locales")
    i18n.SetDefaultLocale("en")

    i18n.Trans("hello.world") // World
    i18n.Locale("zh").Trans("hello.world") // 世界

    // with params
    i18n.Trans("hello.foo", "bar") // foo bar
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type I18n

type I18n struct {
	// contains filtered or unexported fields
}

func New

func New(fs embed.FS, path string) (*I18n, error)

New returns an I18n instance

func (*I18n) Locale

func (i *I18n) Locale(locale string) *translator.Translator

Locale returns the translator instance by the given locale

func (*I18n) SetDefaultLocale

func (i *I18n) SetDefaultLocale(local string)

SetDefaultLocale set the default locale

func (*I18n) Trans

func (i *I18n) Trans(key string, args ...interface{}) string

Trans returns language translation by the given key

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL