i18n

package module
v0.0.0-...-c335968 Latest Latest
Warning

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

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

README

go-typed-i18n

Internationalization for Go programs leveraging the type system.

Comparison to other libraries

When looking around for Go i18n libraries, I found two popular libraries:

  • github.com/nicksnyder/go-i18n

    • workflow styled after gettext, complete with message extractors and message file updaters (mergers).
    • verbose API
    • plain (English) language strings in source code
  • github.com/kataras/i18n

    • stringly typed message IDs
    • no workflow defined

TODO

(when the need arises)

  • extractor and merger program
  • plural support

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Catalog

type Catalog interface {
	LoadMessages(languageCode string, textDomain string, messages any) error
}

Catalog is the work horse doing the actual message loading.

func NewFilesystemCatalog

func NewFilesystemCatalog(files fs.FS, messagesDirectory string) Catalog

NewFilesystemCatalog.

Message files are located in messages directory.

type FilesystemCatalog

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

FilesystemCatalog loads messages from JSON files.

File names must be: <textDomain>.<languageCode>.json. Nested directories are not supported.

func (*FilesystemCatalog) LoadMessages

func (f *FilesystemCatalog) LoadMessages(
	languageCode string,
	textDomain string,
	messages any,
) error

type Translator

type Translator func(textDomain string, messages any) error

Translator loads messages into a struct of your choosing.

Text domain groups messages. There should be one text domain per message struct type.

func NewTranslator

func NewTranslator(languageCode string, catalog Catalog) Translator

NewTranslator creates a translator loading messages from the catalog with the given language code.

Jump to

Keyboard shortcuts

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