i18n

package module
Version: v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2020 License: MIT Imports: 9 Imported by: 25

README

i18n middleware

This Buffalo middleware enables i18n features in your app:

  • User language detection from configurable sources
  • Translation helper using locales bundles from github.com/nicksnyder/go-i18n
  • Localized views

Installation

This middleware is setup by default on a new Buffalo app:

actions/app.go

var app *buffalo.App

// T is used to provide translations
var T *i18n.Translator

// App is where all routes and middleware for buffalo
// should be defined. This is the nerve center of your
// application.
func App() *buffalo.App {
    if app == nil {
        // [...]

        // Setup and use translations:
	var err error
	if T, err = i18n.New(packr.NewBox("../locales"), "en"); err != nil {
		app.Stop(err)
	}
	app.Use(T.Middleware())
    }
    return app
}

Use i18n.New to create a new instance of the translation module, then add the middleware (T.Middleware()) to the app to enable its features.

See https://gobuffalo.io/docs/localization for further info about Buffalo translation features and configuration.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CookieLanguageExtractor

func CookieLanguageExtractor(o LanguageExtractorOptions, c buffalo.Context) []string

CookieLanguageExtractor is a LanguageExtractor implementation, using a cookie.

func HeaderLanguageExtractor

func HeaderLanguageExtractor(o LanguageExtractorOptions, c buffalo.Context) []string

HeaderLanguageExtractor is a LanguageExtractor implementation, using a HTTP Accept-Language header.

func SessionLanguageExtractor

func SessionLanguageExtractor(o LanguageExtractorOptions, c buffalo.Context) []string

SessionLanguageExtractor is a LanguageExtractor implementation, using a session.

func URLPrefixLanguageExtractor

func URLPrefixLanguageExtractor(o LanguageExtractorOptions, c buffalo.Context) []string

URLPrefixLanguageExtractor is a LanguageExtractor implementation, using a prefix in the URL.

Types

type LanguageExtractor

type LanguageExtractor func(LanguageExtractorOptions, buffalo.Context) []string

LanguageExtractor can be implemented for custom finding of search languages. This can be useful if you want to load a user's language from something like a database. See Middleware() for more information on how the default implementation searches for languages.

type LanguageExtractorOptions

type LanguageExtractorOptions map[string]interface{}

LanguageExtractorOptions is a map of options for a LanguageExtractor.

type Translator

type Translator struct {
	// Box - where are the files?
	Box packd.Box
	// DefaultLanguage - default is passed as a parameter on New.
	DefaultLanguage string
	// HelperName - name of the view helper. default is "t"
	HelperName string
	// LanguageExtractors - a sorted list of user language extractors.
	LanguageExtractors []LanguageExtractor
	// LanguageExtractorOptions - a map with options to give to LanguageExtractors.
	LanguageExtractorOptions LanguageExtractorOptions
}

Translator for handling all your i18n needs.

func New

func New(box packd.Box, language string) (*Translator, error)

New Translator. Requires a packr.Box that points to the location of the translation files, as well as a default language. This will also call t.Load() and load the translations from disk.

func (*Translator) AddTranslation

func (t *Translator) AddTranslation(lang *language.Language, translations ...translation.Translation)

AddTranslation directly, without using a file. This is useful if you wish to load translations from a database, instead of disk.

func (*Translator) AvailableLanguages

func (t *Translator) AvailableLanguages() []string

AvailableLanguages gets the list of languages provided by the app.

func (*Translator) Load

func (t *Translator) Load() error

Load translations from the t.Box.

func (*Translator) Middleware

func (t *Translator) Middleware() buffalo.MiddlewareFunc

Middleware for loading the translations for the language(s) selected. By default languages are loaded in the following order:

Cookie - "lang" Session - "lang" Header - "Accept-Language" Default - "en-US"

These values can be changed on the Translator itself. In development model the translation files will be reloaded on each request.

func (*Translator) Refresh

func (t *Translator) Refresh(c buffalo.Context, newLang string)

Refresh updates the context, reloading translation functions. It can be used after language change, to be able to use translation functions in the new language (for a flash message, for instance).

func (*Translator) Translate

func (t *Translator) Translate(c buffalo.Context, translationID string, args ...interface{}) string

Translate returns the translation of the string identified by translationID.

See https://github.com/gobuffalo/i18n-mw/internal/go-i18n

If there is no translation for translationID, then the translationID itself is returned. This makes it easy to identify missing translations in your app.

If translationID is a non-plural form, then the first variadic argument may be a map[string]interface{} or struct that contains template data.

If translationID is a plural form, the function accepts two parameter signatures 1. T(count int, data struct{}) The first variadic argument must be an integer type (int, int8, int16, int32, int64) or a float formatted as a string (e.g. "123.45"). The second variadic argument may be a map[string]interface{} or struct{} that contains template data. 2. T(data struct{}) data must be a struct{} or map[string]interface{} that contains a Count field and the template data, Count field must be an integer type (int, int8, int16, int32, int64) or a float formatted as a string (e.g. "123.45").

func (*Translator) TranslateWithLang

func (t *Translator) TranslateWithLang(lang, translationID string, args ...interface{}) (string, error)

TranslateWithLang returns the translation of the string identified by translationID, for the given language. See Translate for further details.

Source Files

Directories

Path Synopsis
internal
go-i18n/goi18n
The goi18n command formats and merges translation files.
The goi18n command formats and merges translation files.
go-i18n/i18n
Package i18n supports string translations with variable substitution and CLDR pluralization.
Package i18n supports string translations with variable substitution and CLDR pluralization.
go-i18n/i18n/bundle
Package bundle manages translations for multiple languages.
Package bundle manages translations for multiple languages.
go-i18n/i18n/language
Package language defines languages that implement CLDR pluralization.
Package language defines languages that implement CLDR pluralization.
go-i18n/i18n/translation
Package translation defines the interface for a translation.
Package translation defines the interface for a translation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL