gg_i18n_bundle

package
v0.2.107 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: BSD-3-Clause Imports: 5 Imported by: 2

README

G&G i18n

G&G i18n is a Go package that helps you translate Go programs into multiple languages using a Mustache like syntax.

Example

en.json:

This is an i18n resource file. This file contains some labels with both simple text and plurals.

NOTE: {{rows-length}} is not a model tag. This tag has been injected from LyGo Ext i18n.

{
  "i18n-title": "A sample template",
  "i18n-description": "{{rows-index}}) Hello, my name is {{name}} {{surname}}",
  "i18n-rows": {
    "one": "There is {{rows-length}} row",
    "other": "There are {{rows-length}} rows"
  }
}

model.json:

This is a sample data model passed to localizer. The model contains some tags that are references to i18n resource file ("{{i18n-description}}"). Those tags will be localized and translated.

{
  "not_localized": "<h1>ROWS</h1>",
  "rows": [
    {
      "name": "Mario",
      "surname": "Rossi",
      "description": "{{i18n-description}}"
    },
    {
      "name": "Sergio",
      "surname": "Bianchi",
      "description": "{{i18n-description}}"
    }
  ]
}

template.html:

This is a typical Mustache Template. The template contains both i18 tags ("{{i18n-title}}, {{i18n-rows}}") and model tags ("{{{not_localized}}}", "{{description}}"). All this tags will be translated.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{i18n-title}}</title>
</head>
<body>
    {{{not_localized}}}
    <h2>{{i18n-rows}}</h2>
    {{#rows}}
    <div>{{description}}</div>
    {{/rows}}
</body>
</html>

main.go:

package main

import (
	"bitbucket.org/lygo/lygo_commons/lygo_io"
	"bitbucket.org/lygo/lygo_commons/lygo_json"
	"bitbucket.org/lygo/lygo_ext_i18n/lygo_i18n"
	"fmt"
)

func main(){
    html, err := lygo_io.ReadTextFromFile("./template/template.html")
    if nil!=err{
        panic(err)
    }
    model, err := lygo_json.ReadMapFromFile("./template/model.json")
    if nil!=err{
    	panic(err)
    }

    // creates a i18n bundle with "it" as default language
    bundle, err := lygo_i18n.NewBundleFromDir("it", "./template")
    if nil!=err{
    	panic(err)
    }
    
    // creates a localizer to translate a text template
    localizer := lygo_i18n.NewLocalizer(bundle)
    localized, err := localizer.Localize("it", html, model)
    if nil != err {
        panic(err)
    }
    fmt.Println(localized)
}

output:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>A sample template</title>
</head>
<body>
    <h1>ROWS</h1>
    <h2>There are 2 rows</h2>
    <div>0) Hello, my name is Mario Rossi</div>
    <div>1) Hello, my name is Sergio Bianchi</div>
</body>
</html>

Documentation

Index

Constants

View Source
const (
	SUFFIX_SEP    = "_"
	SUFFIX_LENGHT = "_length"
	SUFFIX_INDEX  = "_index"
)

Variables

View Source
var (
	LanguageNotSupportedError      = errors.New("language_not_supported")
	DeserializerNotRegisteredError = errors.New("deserializer_not_registered")
	ResourceNotFoundError          = errors.New("resource_not_found")
)
View Source
var (
	PLURAL_FIELDS = []string{"value", "count", "values", "qty", "length", "len", "size", "num"}
)

Functions

func DeserializeFile

func DeserializeFile(filename string) (map[string]*Element, error)

func RegisterDeserializer

func RegisterDeserializer(extension string, deserializer Deserializer)

Types

type Bundle

type Bundle struct {
	Strictly                   bool // must load data or go panic
	IgnoreUnknownDeserializers bool
	// contains filtered or unexported fields
}

func NewBundleFromDir

func NewBundleFromDir(defLang interface{}, path string) (*Bundle, error)

func NewBundleFromFiles

func NewBundleFromFiles(defLang interface{}, files []string) (*Bundle, error)

func NewBundleFromLang

func NewBundleFromLang(defLang interface{}) (*Bundle, error)

func NewCustomBundle

func NewCustomBundle(defLang string, deserializers *map[string]Deserializer) (*Bundle, error)

func NewCustomBundleFromTag

func NewCustomBundleFromTag(defTag language.Tag, deserializers *map[string]Deserializer) *Bundle

func (*Bundle) AddElement

func (instance *Bundle) AddElement(rawLang string, path string, elem *Element)

func (*Bundle) Deserializers

func (instance *Bundle) Deserializers() []string

func (*Bundle) Get

func (instance *Bundle) Get(lang string, path string, data ...interface{}) (string, error)

func (*Bundle) GetDictionary

func (instance *Bundle) GetDictionary(rawLang string) (map[string]*Element, error)

func (*Bundle) GetDictionaryByTag

func (instance *Bundle) GetDictionaryByTag(tag language.Tag) (map[string]*Element, error)

func (*Bundle) GetElement

func (instance *Bundle) GetElement(lang string, path string) (*Element, error)

func (*Bundle) GetWithPlural

func (instance *Bundle) GetWithPlural(lang string, path string, pluralFields []string,
	data ...interface{}) (string, error)

func (*Bundle) GetWithPluralValue

func (instance *Bundle) GetWithPluralValue(lang string, path string, value float64,
	data ...interface{}) (string, error)

func (*Bundle) LoadAll

func (instance *Bundle) LoadAll(dirName string) error

func (*Bundle) LoadFile

func (instance *Bundle) LoadFile(file string) error

func (*Bundle) LoadFiles

func (instance *Bundle) LoadFiles(files []string) error

func (*Bundle) Renderer

func (instance *Bundle) Renderer() I18nRenderer

func (*Bundle) SetRenderer

func (instance *Bundle) SetRenderer(renderer I18nRenderer)

func (*Bundle) String

func (instance *Bundle) String() string

type Deserializer

type Deserializer func(data []byte) (map[string]*Element, error)

type Element

type Element struct {
	Zero  string `json:"zero"`
	One   string `json:"one"`
	Two   string `json:"two"`
	Other string `json:"other"`
}

func NewElementFromMap

func NewElementFromMap(m map[string]interface{}) (*Element, error)

func NewElementFromString

func NewElementFromString(s string) *Element

func (*Element) Plural

func (instance *Element) Plural(value float64) string

func (*Element) PluralValueWithFields

func (instance *Element) PluralValueWithFields(pluralFields []string, model ...interface{}) float64

func (*Element) Value

func (instance *Element) Value() string

type I18NHelper

type I18NHelper struct {
}
var I18N *I18NHelper

func (*I18NHelper) NewLocalizer

func (instance *I18NHelper) NewLocalizer(args ...interface{}) (localizer *Localizer, err error)

type I18nRenderer

type I18nRenderer interface {
	Render(text string, model ...interface{}) (string, error)
	TagsFrom(text string) ([]string, error)
}

type Localizer

type Localizer struct {
	// contains filtered or unexported fields
}

func NewLocalizer

func NewLocalizer(bundle *Bundle) *Localizer

func (*Localizer) Localize

func (instance *Localizer) Localize(rawLang string, text string, context ...interface{}) (string, error)

func (*Localizer) Renderer

func (instance *Localizer) Renderer() I18nRenderer

func (*Localizer) SetRenderer

func (instance *Localizer) SetRenderer(renderer I18nRenderer)

func (*Localizer) String

func (instance *Localizer) String() string

type Renderer

type Renderer struct {
}

func (*Renderer) Render

func (instance *Renderer) Render(text string, model ...interface{}) (string, error)

func (*Renderer) TagsFrom

func (instance *Renderer) TagsFrom(text string) ([]string, error)

Jump to

Keyboard shortcuts

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