i18n

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2023 License: MIT Imports: 10 Imported by: 1

README

i18n

GitHub Workflow Status Codecov GoDoc Sourcegraph

Package i18n is a middleware that provides internationalization and localization for Flamego.

Installation

The minimum requirement of Go is 1.18.

go get github.com/flamego/i18n

Getting started

# locales/locale_en-US.ini
greeting = How are you?
# locales/locale_zh-CN.ini
greeting = 你好吗?
package main

import (
	"github.com/flamego/flamego"
	"github.com/flamego/i18n"
)

func main() {
	f := flamego.Classic()
	f.Use(i18n.I18n(
		i18n.Options{
			Languages: []i18n.Language{
				{Name: "en-US", Description: "English"},
				{Name: "zh-CN", Description: "简体中文"},
			},
		},
	))
	f.Get("/", func(l i18n.Locale) {
		message := l.Translate("greeting")
		// ...
	})
	f.Run()
}

Getting help

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func I18n

func I18n(opt Options) flamego.Handler

I18n returns a middleware handler that injects i18n.Locale into the request context, which is used for localization.

Types

type CookieOptions

type CookieOptions struct {
	// Name is the name of the cookie. Default is "lang".
	Name string
	// Path is the Path attribute of the cookie. Default is "/".
	Path string
	// Domain is the Domain attribute of the cookie. Default is not set.
	Domain string
	// MaxAge is the MaxAge attribute of the cookie. Default is math.MaxInt.
	MaxAge int
	// Secure specifies whether to set Secure for the cookie.
	Secure bool
	// HTTPOnly specifies whether to set HTTPOnly for the cookie.
	HTTPOnly bool
	// SameSite is the SameSite attribute of the cookie. Default is
	// http.SameSiteLaxMode.
	SameSite http.SameSite
}

CookieOptions contains options for setting HTTP cookies.

type Language

type Language struct {
	// Name is the BCP 47 language name, e.g. "en-US".
	Name string
	// Description is the descriptive name of the language, e.g. "English".
	Description string
}

Language contains the name and description of a language.

type Locale

type Locale interface {
	// Lang returns the BCP 47 language name of the locale.
	Lang() string
	// Description returns the descriptive name of the locale.
	Description() string
	// Translate translates the message of the given key. It falls back to use the
	// "Default" to translate if the given key does not exist in the current locale.
	Translate(key string, args ...interface{}) string
}

Locale is the message translator of a language.

type Options

type Options struct {
	// FileSystem is the interface for supporting any implementation of the
	// http.FileSystem.
	FileSystem http.FileSystem
	// Directory is the primary directory to load locale files. This value is
	// ignored when FileSystem is set. Default is "locales".
	Directory string
	// AppendDirectories is a list of additional directories to load locale files
	// for overwriting locale files that are loaded from FileSystem or Directory.
	AppendDirectories []string
	// Languages is the list of languages to load locale files for.
	Languages []Language
	// Default is the name of the default language to fall back for missing
	// translations. Default is the first element of Languages.
	Default string
	// NameFormat is the name format of locale files. Default is "locale_%s.ini".
	NameFormat string
	// QueryParameter is the name of the URL query parameter to accept language
	// override. Default is "lang".
	QueryParameter string
	// Cookie is a set of options for setting HTTP cookies.
	Cookie CookieOptions
}

Options contains options for the i18n.I18n middleware.

Jump to

Keyboard shortcuts

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