i18n

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 11 Imported by: 0

README

i18n

i18n is a package that helps you translate Go programs into multiple languages.

Features

  • plural

Installation

go install git.local/go/i18n@latest

How to use

First you must create a json language file (see Translation file structure for details).

Load all translation files and set a fallback language. If you don't need a fallback language, you can set it as language.Und.

i18n/en/main.json

[{ "id": "hello", "message": "Hello, world!" }]

i18n/ru/main.json

[{ "id": "hello", "message": "Здравствуй, Мир!" }]

main.go

package main

import (
  "fmt"
  "log"

  "github.com/yarigo/i18n"
  "golang.org/x/text/language"
)

func main() {
  // Create a new instance.
  t := i18n.New(&i18n.Config{Path: "./i18n", Fallback: language.Und})

  // Load all translation files.
  if err := t.Load(); err != nil {
    log.Fatalln(err.Error())
  }

  // Take printer for English language.
  en := t.Printer(language.English)
  // Take printer for Russian language.
  ru := t.Printer(language.Russian)

  // Print message.
  fmt.Println(en.Sprintf("hello"))
  // Print message.
  fmt.Println(ru.Sprintf("hello"))
}
$ go run main.go
Hello, world!
Здравствуй, Мир!
plural

A selector matches an argument if:

  • it is "other" or Other
  • it matches the plural form of the argument: "zero", "one", "two", "few", or "many", or the equivalent Form
  • it is of the form "=x" where x is an integer that matches the value of the argument.
  • it is of the form "<x" where x is an integer that is larger than the argument.

For example:

"rules": {
  "1": {
    "=0": "...",
    "one": "..."
  },
  "2": {
    ">10": "...",
    "other": "..."
  },
}

1 and 2 its a index of variable.

Translation file structure

One of the message or rules fields is required, but not both.

Without plural
[
  {
    "id": "unique message id",
    "message": "message text"
  }
]
With plural
[
  {
    "id": "unique message id",
    "rules": {
      "1": {
        "one": "first message id",
        "many": "message id %d"
      }
    }
  }
]

or

[
  {
    "id": "unique message id",
    "rules": {
      "2": {
        "one": "message id %d and its %d text message",
        "many": "message id %d and message text %d"
      }
    }
  }
]

1 and 2 are the number of the argument to use in the plural. If you only use the first argument, you can skip defining it like this:

[
  {
    "id": "unique message id",
    "rules": {
      "one": "first message id",
      "many": "message id %d"
    }
  }
]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Path to the languages folder.
	// File or directory name use as language tag.
	Path string
	// Fallback language.
	Fallback language.Tag
}

Config of i18n.

type ErrorFallbackTagNotExists

type ErrorFallbackTagNotExists struct {
	Tag language.Tag
}

ErrorFallbackTagNotExists reports that translation files for fallback language do not exist.

func (*ErrorFallbackTagNotExists) Error

func (i *ErrorFallbackTagNotExists) Error() string

Error message.

type ErrorLanguageTagAlreadyExists

type ErrorLanguageTagAlreadyExists struct {
	Tag language.Tag
}

ErrorLanguageTagAlreadyExists reports a duplicate language tag.

func (*ErrorLanguageTagAlreadyExists) Error

Error message.

type ErrorMessageValidate

type ErrorMessageValidate struct {
	Tag       language.Tag
	FilePath  string
	MessageID string
	Message   string
}

ErrorMessageValidate reports a validation error.

func (*ErrorMessageValidate) Error

func (i *ErrorMessageValidate) Error() string

Error message.

type I18n

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

I18n data.

func New

func New(cfg *Config) *I18n

New instance of i18n.

func (*I18n) Load

func (i *I18n) Load() (err error)

Load all locales.

func (*I18n) Printer

func (i *I18n) Printer(tag language.Tag) *message.Printer

Printer implements language-specific formatted I/O analogous to the fmt package.

Jump to

Keyboard shortcuts

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