Localization

Translating your app is an effective way to make it understandable to the many people around the globe! Buffalo uses the go-i18n project to provide the i18n (providing default text, often in english) and l10n (providing translation for the other supported languages) of your app.

Markup the translatable strings

This document only applies when using https://github.com/gobuffalo/buffalo/render.
Please see github.com/gobuffalo/plush for more details on the underlying templating package.

Translatable strings must use a specific markup to allow the l10n engine to get the translations.

In a plush template, you can use the t (singular strings) and since v0.10.0 the tp (singular & plural) helpers:

<%= t("greetings") %>

since v0.10.0

<%= tp("messages-notification", notificationsLen) %>

Provide translations

Translations are stored in the locales folder. By default, they are stored in a all.en-us.yaml file for the american english strings.

You can provide translations for another language by providing a new file all.my-language-code.yaml. If you want to split your strings into logical modules, you can even create multiples files, e.g. users.en-us.yaml for the user-related stuff, and all.en-us.yaml for the global stuff.

The localization format used by go-i18n is the following:

- id: greetings
  translation: "Welcome to Buffalo (EN)"

- id: messages-notification
  translation:
    one: "You have {{.Count}} notification"
    other: "You have {{.Count}} notifications"

Define the default language

To define the default language of your app, you need to edit the app.go file in the actions folder:

// Setup and use translations:
var err error
if T, err = i18n.New(packr.NewBox("../locales"), "en-US"); err != nil {
  app.Stop(err)
}
app.Use(T.Middleware())

Changing "en-US" to another language code will change the default language.

Copyright © 2017 Buffalo. All rights reserved.

Buffalo Gopher designed by Ashley McNamara — Original Gopher designed by Renee French.