i18n

package
v0.0.0-...-c72e3f0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2015 License: MIT Imports: 15 Imported by: 0

README

I18n

I18n package support translations with different backends, like database, YAML.

Usage

import (
  "github.com/jinzhu/gorm"
  "github.com/qor/qor/i18n"
  "github.com/qor/qor/i18n/backends/database"
)

func main() {
  db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")

  // Using two backends, early backend has higher priority
  I18n = i18n.New(database.New(&db), yaml.New(filepath.Join(config.Root, "config/locales")))

  // Add Translation
  I18n.AddTranslation(&i18n.Translation{Key: "hello-world", Locale: "zh-CN", Value: "Hello World"})

  // Update Translation
  I18n.SaveTranslation(&i18n.Translation{Key: "hello-world", Locale: "zh-CN", Value: "Hello World"})

  // Delete Translation
  I18n.DeleteTranslation(&i18n.Translation{Key: "hello-world", Locale: "zh-CN", Value: "Hello World"})

  // Read transation with key `hello-world`
  I18n.T("zh-CN", "hello-world")

  // Read transation with `Scope`
  I18n.Scope("home-page").T("zh-CN", "hello-world") // read translation with translation key `home-page.hello-world`

  // Read transation with `Default Value`
  I18n.Scope("home-page").Default("Hello World").T("zh-CN", "not-existing") // Will return default value `Hello World`
}

Advanced Usage

// Using on frontend - you could define a T method then using in template
// <h2>{{T "home_page.how_it_works" "HOW DOES IT WORK? {{$1}}" "It is work" }}</h2>
func T(key string, value string, args ...interface{}) string {
	return config.Config.I18n.Default(value).T("en-US", key, args)
}

// Interpolation - i18n using golang template to parse translations with interpolation variable

I18n.AddTranslation(&i18n.Translation{Key: "hello", Locale: "en-US", Value: "Hello {{.Name}}"})
type User struct {
  Name string
}
I18n.T("en-US", "hello", User{Name: "jinzhu"}) //=> Hello jinzhu

// Pluralization - i18n is using [cldr](https://github.com/theplant/cldr) to do the job, it provide functions `p`, `zero`, `one`, `two`, `few`, `many`, `other` for pluralization, refer it for more details

I18n.AddTranslation(&i18n.Translation{Key: "count", Locale: "en-US", Value: "{{p "Count" (one "{{.Count}} item") (other "{{.Count}} items")}}"})
I18n.T("en-US", "count", map[string]int{"Count": 1}) //=> 1 item

// Ordered Params
I18n.AddTranslation(&i18n.Translation{Key: "ordered_params", Locale: "en-US", Value: "{{$1}} {{$2}} {{$1}}"})
I18n.T("en-US", "ordered_params", "string1", "string2") //=> string1 string2 string1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = "en-US"

Functions

func RenderInlineEditAssets

func RenderInlineEditAssets(isIncludeJQuery bool, isIncludeExtendAssetLib bool) (template.HTML, error)

Using: http://vitalets.github.io/x-editable/index.html You could use Bootstrap or JQuery UI by set isIncludeExtendAssetLib to false and load files by yourself

Types

type Backend

type Backend interface {
	LoadTranslations() []*Translation
	SaveTranslation(*Translation) error
	DeleteTranslation(*Translation) error
}

type I18n

type I18n struct {
	Backends     []Backend
	Translations map[string]map[string]*Translation
	IsInlineEdit bool
	// contains filtered or unexported fields
}

func New

func New(backends ...Backend) *I18n

func (*I18n) AddTranslation

func (i18n *I18n) AddTranslation(translation *Translation)

func (*I18n) ConfigureQorResource

func (i18n *I18n) ConfigureQorResource(res resource.Resourcer)

func (*I18n) Default

func (i18n *I18n) Default(value string) admin.I18n

func (*I18n) DeleteTranslation

func (i18n *I18n) DeleteTranslation(translation *Translation) error

func (*I18n) EnableInlineEdit

func (i18n *I18n) EnableInlineEdit(isInlineEdit bool) admin.I18n

func (I18n) ResourceName

func (I18n) ResourceName() string

func (*I18n) SaveTranslation

func (i18n *I18n) SaveTranslation(translation *Translation) error

func (*I18n) Scope

func (i18n *I18n) Scope(scope string) admin.I18n

func (*I18n) T

func (i18n *I18n) T(locale, key string, args ...interface{}) template.HTML

type Translation

type Translation struct {
	Key     string
	Locale  string
	Value   string
	Backend Backend
}

Directories

Path Synopsis
backends

Jump to

Keyboard shortcuts

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