Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
backends
exchange_actions
inline_edit
views/themes/i18n
.DS_Store
.gitignore
LICENSE
README.md
controller.go
i18n.go
i18n_test.go

README.md

I18n

I18n provides internationalization support for your application, it supports different storage solutions (backends) including a SQL database and YAML.

GoDoc

Usage

import (
  "github.com/jinzhu/gorm"
  "github.com/mmanjoura/adsa-cms/i18n"
  "github.com/mmanjoura/adsa-cms/i18n/backends/database"
)

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

  // Using two backends, earlier backend has higher priority
  I18n = i18n.New(
    database.New(&db), // load translations from database
    yaml.New(filepath.Join(config.Root, "config/locales")),  // load translations from YAML files in directory `config/locales
  )

  // Add Translation
  I18n.AddTranslation(&i18n.Translation{Key: "hello-world", Locale: "en-US", Value: "hello world"})

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

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

  // Read Translation with key `hello-world`
  I18n.T("en-US", "hello-world")

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

  // Read Translation with `Default Value`
  I18n.Default("Default Value").T("zh-CN", "non-existing-key") // Will return default value `Default Value`
}

Interpolation

I18n utilises 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 utilises cldr to achieve pluralization, it provides the functions p, zero, one, two, few, many, other for this purpose. Refer to cldr documentation for more information.

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

Integrate with Golang Templates

You could define a t method and register it as FuncMap:

var I18n *i18n.I18n

func init() {
  I18n = i18n.New(database.New(&db), yaml.New(filepath.Join(config.Root, "config/locales")))
}

func T(key string, value string, args ...interface{}) string {
	return I18n.Default(value).T("en-US", key, args...)
}

// then use it like
{{t "home_page.how_it_works" "HOW DOES IT WORK? {{$1}}" "It works" }}

QOR Support

QOR is architected from the ground up to accelerate development and deployment of Content Management Systems, E-commerce Systems, and Business Applications and as such is comprised of modules that abstract common features for such systems.

Although I18n can be used standalone, it works very nicely with QOR - if you have requirements to manage your application's data, be sure to check QOR out!

To use I18n with QOR, simply add it as resource to the admin:

 Admin.AddResource(I18n)

QOR Demo: http://demo.getqor.com/admin

I18n Demo with QOR

Inline Edit

You could manage translations with QOR Admin interface after register it into QOR Admin, But usually hard to translate a translation without its context, the inline edit is made to resolve the problem

With it, you could manage translations from frontend, just like Integrate with Golang Templates, you need to register a func map for Golang templates to render inline editable translations.

The good thing is we have created a package do the job for you, it will generate a funcmap, you just need to use it when parsing your templates

// `I18n` hold translations backends
// `en-US` current locale
// `true` enable inline edit mode or not, if inline edit not enabled, it works just like the funcmap in section "Integrate with Golang Templates"
inline_edit.FuncMap(I18n, "en-US", true) // => map[string]interface{}{
                                         //     "t": func(string, ...interface{}) template.HTML {
                                         //        // ...
                                         //      },
                                         //    }

License

Released under the MIT License.

You can’t perform that action at this time.