i18n

package
v3.0.0-rc.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2016 License: Apache-2.0 Imports: 3 Imported by: 0

README

Middleware information

This folder contains a middleware for internationalization uses a third-party package named i81n.

More can be found here: https://github.com/Unknwon/i18n

Description

Package i18n is for app Internationalization and Localization.

How to use

Create folder named 'locales'

///Files: 

./locales/locale_en-US.ini 
./locales/locale_el-US.ini 

Contents on locale_en-US:

hi = hello, %s

Contents on locale_el-GR:

hi = ����, %s

package main

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/middleware/i18n"
)

func main() {

	iris.UseFunc(i18n.I18n(i18n.Options{Default: "en-US",
		Languages: map[string]string{
			"en-US": "./locales/locale_en-US.ini",
			"el-GR": "./locales/locale_el-GR.ini",
			"zh-CN": "./locales/locale_zh-CN.ini"}}))	
	// or iris.Use(i18n.I18nHandler(....))
	// or iris.Get("/",i18n.I18n(....), func (ctx *iris.Context){}) 
		
	iris.Get("/", func(ctx *iris.Context) {
		hi := ctx.GetFmt("translate")("hi", "maki") // hi is the key, 'maki' is the %s, the second parameter is optional
		language := ctx.Get("language") // language is the language key, example 'en-US'

		ctx.Write("From the language %s translated output: %s", language, hi)
	})
	
	
	println("Server is running at :8080")
	iris.Listen(":8080")

}

For a working example, click here

Documentation

Index

Constants

View Source
const AcceptLanguage = "Accept-Language"

AcceptLanguage is the Header key "Accept-Language"

Variables

This section is empty.

Functions

func I18n

func I18n(_options ...Options) iris.HandlerFunc

I18n returns the middleware as iris.HandlerFunc with the passed options

func I18nHandler

func I18nHandler(_options ...Options) *i18nMiddleware

I18nHandler returns the middleware which is just an iris.handler

Types

type Options

type Options struct {
	// Default set it if you want a default language
	//
	// Checked: Configuration state, not at runtime
	Default string
	// URLParameter is the name of the url parameter which the language can be indentified
	//
	// Checked: Serving state, runtime
	URLParameter string
	// Languages is a map[string]string which the key is the language i81n and the value is the file location
	//
	// Example of key is: 'en-US'
	// Example of value is: './locales/en-US.ini'
	Languages map[string]string
}

Options the i18n options

Jump to

Keyboard shortcuts

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