i18n

package module
v0.0.0-...-86a1e89 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2017 License: Apache-2.0 Imports: 8 Imported by: 1

README

i18n

Middleware i18n provides app Internationalization and Localization for Macross.

Installation
go get -u github.com/macross-contrib/i18n

Example

package main

import (
	"fmt"
	"github.com/insionng/macross"
	"github.com/macross-contrib/i18n"
)

func main() {
	m := macross.Classic()
	m.Use(i18n.I18n(i18n.Options{
		Directory:   "locale",
		DefaultLang: "zh-CN",
		Langs:       []string{"en-US", "zh-CN"},
		Names:       []string{"English", "简体中文"},
		Redirect:    true,
	}))

	m.Get("/", func(self *macross.Context) error {
		fmt.Println("Header>", self.Request.Header.String())
		return self.String("current language is " + self.Language())
	})

	// Use in handler.
	m.Get("/trans", func(self *macross.Context) error {
		fmt.Println("Header>", self.Request.Header.String())
		return self.String(fmt.Sprintf("hello %s", self.Tr("world")))
	})

	fmt.Println("Listen on 9999")
	m.Listen(9999)
}

Getting Help

License

This project is under the Apache License, Version 2.0. See the LICENSE file for the full license text.

Documentation

Overview

Package i18n is a middleware that provides app Internationalization and Localization of Macross.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func I18n

func I18n(options ...Options) macross.Handler

I18n is a middleware provides localization layer for your application. Paramenter langs must be in the form of "en-US", "zh-CN", etc. Otherwise it may not recognize browser input.

func Version

func Version() string

Types

type LangType

type LangType struct {
	Lang, Name string
}

type Localer

type Localer struct {
	i18n.Locale
}

A Localer describles the information of localization.

func (Localer) Language

func (l Localer) Language() string

Language returns language current locale represents.

type Options

type Options struct {
	// Suburl of path. Default is empty.
	SubURL string
	// Directory to load locale files. Default is "conf/locale"
	Directory string
	// File stores actual data of locale files. Used for in-memory purpose.
	Files map[string][]byte
	// Custom directory to overload locale files. Default is "custom/conf/locale"
	CustomDirectory string
	// Langauges that will be supported, order is meaningful.
	Langs []string
	// Human friendly names corresponding to Langs list.
	Names []string
	// Default language locale, leave empty to remain unset.
	DefaultLang string
	// Locale file naming style. Default is "locale_%s.ini".
	Format string
	// Name of language parameter name in URL. Default is "lang".
	Parameter string
	// Redirect when user uses get parameter to specify language.
	Redirect bool
	// Name that maps into template variable. Default is "i18n".
	TmplName string
	// Configuration section name. Default is "i18n".
	Section string
}

Options represents a struct for specifying configuration options for the i18n middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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