Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: Add TOML support for language files #3257

Merged
merged 4 commits into from
Apr 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions docs/content/content/multilingual.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ The above also uses the `i18n` func, see [Translation of strings](#translation-o

### Translation of strings

Hugo uses [go-i18n](https://github.com/nicksnyder/go-i18n) to support string translations. Follow the link to find tools to manage your translation workflows.
Hugo uses [go-i18n](https://github.com/nicksnyder/go-i18n) to support string translations. Follow the link to find tools to manage your translation workflows.

Translations are collected from the `themes/[name]/i18n/` folder (built into the theme), as well as translations present in `i18n/` at the root of your project. In the `i18n`, the translations will be merged and take precedence over what is in the theme folder. Language files should be named according to RFC 5646 with names such as `en-US.yaml`, `fr.yaml`, etc.
Translations are collected from the `themes/[name]/i18n/` folder (built into the theme), as well as translations present in `i18n/` at the root of your project. In the `i18n`, the translations will be merged and take precedence over what is in the theme folder. Language files should be named according to RFC 5646 with names such as `en-US.toml`, `fr.toml`, etc.

From within your templates, use the `i18n` function like this:

```
{{ i18n "home" }}
```

This uses a definition like this one in `i18n/en-US.yaml`:
This uses a definition like this one in `i18n/en-US.toml`:

```
- id: home
translation: "Home"
[home]
other = "Home"
```

Often you will want to use to the page variables in the translations strings. To do that, pass on the "." context when calling `i18n`:
Expand All @@ -141,19 +141,18 @@ Often you will want to use to the page variables in the translations strings. To
{{ i18n "wordCount" . }}
```

This uses a definition like this one in `i18n/en-US.yaml`:
This uses a definition like this one in `i18n/en-US.toml`:

```
- id: wordCount
translation: "This article has {{ .WordCount }} words."
[wordCount]
other = "This article has {{ .WordCount }} words."
```
An example of singular and plural form:

```
- id: readingTime
translation:
one: "One minute read"
other: "{{.Count}} minutes read"
[readingTime]
one = "One minute read"
other = "{{.Count}} minutes read"
```
And then in the template:

Expand Down
2 changes: 2 additions & 0 deletions examples/multilingual/i18n/en.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[head_title]
other = "Multilingual"
3 changes: 0 additions & 3 deletions examples/multilingual/i18n/en.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions examples/multilingual/i18n/et.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[head_title]
other = "Mitmekeelne"
2 changes: 0 additions & 2 deletions examples/multilingual/i18n/et.yaml

This file was deleted.

4 changes: 3 additions & 1 deletion hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
// TODO(bep)
cfg.Cfg.Set("multilingual", sites[0].multilingualEnabled())

applyDepsIfNeeded(cfg, sites...)
if err := applyDepsIfNeeded(cfg, sites...); err != nil {
return nil, err
}

h.Deps = sites[0].Deps

Expand Down
8 changes: 4 additions & 4 deletions hugolib/hugo_sites_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,17 +1050,17 @@ func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, conf
if err := afero.WriteFile(mf,
filepath.Join("i18n", "en.yaml"),
[]byte(`
- id: hello
translation: "Hello"
hello:
other: "Hello"
`),
0755); err != nil {
t.Fatalf("Failed to write language file: %s", err)
}
if err := afero.WriteFile(mf,
filepath.Join("i18n", "fr.yaml"),
[]byte(`
- id: hello
translation: "Bonjour"
hello:
other: "Bonjour"
`),
0755); err != nil {
t.Fatalf("Failed to write language file: %s", err)
Expand Down
35 changes: 16 additions & 19 deletions i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var i18nTests = []i18nTest{
// All translations present
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"hello\"\n translation: \"Hello, World!\""),
"es.yaml": []byte("- id: \"hello\"\n translation: \"¡Hola, Mundo!\""),
"en.toml": []byte("[hello]\nother = \"Hello, World!\""),
"es.toml": []byte("[hello]\nother = \"¡Hola, Mundo!\""),
},
args: nil,
lang: "es",
Expand All @@ -52,8 +52,8 @@ var i18nTests = []i18nTest{
// Translation missing in current language but present in default
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"hello\"\n translation: \"Hello, World!\""),
"es.yaml": []byte("- id: \"goodbye\"\n translation: \"¡Adiós, Mundo!\""),
"en.toml": []byte("[hello]\nother = \"Hello, World!\""),
"es.toml": []byte("[goodbye]\nother = \"¡Adiós, Mundo!\""),
},
args: nil,
lang: "es",
Expand All @@ -64,8 +64,8 @@ var i18nTests = []i18nTest{
// Translation missing in default language but present in current
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"goodybe\"\n translation: \"Goodbye, World!\""),
"es.yaml": []byte("- id: \"hello\"\n translation: \"¡Hola, Mundo!\""),
"en.toml": []byte("[goodybe]\nother = \"Goodbye, World!\""),
"es.toml": []byte("[hello]\nother = \"¡Hola, Mundo!\""),
},
args: nil,
lang: "es",
Expand All @@ -76,8 +76,8 @@ var i18nTests = []i18nTest{
// Translation missing in both default and current language
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"goodbye\"\n translation: \"Goodbye, World!\""),
"es.yaml": []byte("- id: \"goodbye\"\n translation: \"¡Adiós, Mundo!\""),
"en.toml": []byte("[goodbye]\nother = \"Goodbye, World!\""),
"es.toml": []byte("[goodbye]\nother = \"¡Adiós, Mundo!\""),
},
args: nil,
lang: "es",
Expand All @@ -88,7 +88,7 @@ var i18nTests = []i18nTest{
// Default translation file missing or empty
{
data: map[string][]byte{
"en.yaml": []byte(""),
"en.toml": []byte(""),
},
args: nil,
lang: "es",
Expand All @@ -99,8 +99,8 @@ var i18nTests = []i18nTest{
// Context provided
{
data: map[string][]byte{
"en.yaml": []byte("- id: \"wordCount\"\n translation: \"Hello, {{.WordCount}} people!\""),
"es.yaml": []byte("- id: \"wordCount\"\n translation: \"¡Hola, {{.WordCount}} gente!\""),
"en.toml": []byte("[wordCount]\nother = \"Hello, {{.WordCount}} people!\""),
"es.toml": []byte("[wordCount]\nother = \"¡Hola, {{.WordCount}} gente!\""),
},
args: struct {
WordCount int
Expand All @@ -114,22 +114,19 @@ var i18nTests = []i18nTest{
},
}

func doTestI18nTranslate(t *testing.T, data map[string][]byte, lang, id string, args interface{}, cfg config.Provider) string {
func doTestI18nTranslate(t *testing.T, test i18nTest, cfg config.Provider) string {
i18nBundle := bundle.New()

for file, content := range data {
for file, content := range test.data {
err := i18nBundle.ParseTranslationFileBytes(file, content)
if err != nil {
t.Errorf("Error parsing translation file: %s", err)
}
}

translator := NewTranslator(i18nBundle, cfg, logger)

f := translator.Func(lang)

translated := f(id, args)

f := translator.Func(test.lang)
translated := f(test.id, test.args)
return translated
}

Expand All @@ -148,7 +145,7 @@ func TestI18nTranslate(t *testing.T) {
} else {
expected = test.expected
}
actual = doTestI18nTranslate(t, test.data, test.lang, test.id, test.args, v)
actual = doTestI18nTranslate(t, test, v)
require.Equal(t, expected, actual)
}
}
Expand Down
16 changes: 8 additions & 8 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,22 @@
"revisionTime": "2016-12-11T22:23:15Z"
},
{
"checksumSHA1": "UW57eu+Se/KHPD+SoNJLIERYHZQ=",
"checksumSHA1": "K1cQRPixkKsqOEYC/7PmbAO4KuY=",
"path": "github.com/nicksnyder/go-i18n/i18n/bundle",
"revision": "f757c9f9b69c16ff69d38dbf224be28a7b6537bb",
"revisionTime": "2016-12-02T02:38:45Z"
"revision": "7e6a19b81bf3b5a1b480fd176a853b2ac4803be5",
"revisionTime": "2017-04-02T15:44:13Z"
},
{
"checksumSHA1": "+XOg99I1zdmBRUb04ZswvzQ2WS0=",
"path": "github.com/nicksnyder/go-i18n/i18n/language",
"revision": "f757c9f9b69c16ff69d38dbf224be28a7b6537bb",
"revisionTime": "2016-12-02T02:38:45Z"
"revision": "fed5740db6b83ee8ba7c1b07ff063115bc6f4b96",
"revisionTime": "2017-04-01T20:06:44Z"
},
{
"checksumSHA1": "nhlpSPeAP6jMGAuLPM2xflAZTlo=",
"checksumSHA1": "WZOU406In2hs8FJOHWqV8PWkJKs=",
"path": "github.com/nicksnyder/go-i18n/i18n/translation",
"revision": "f757c9f9b69c16ff69d38dbf224be28a7b6537bb",
"revisionTime": "2016-12-02T02:38:45Z"
"revision": "fed5740db6b83ee8ba7c1b07ff063115bc6f4b96",
"revisionTime": "2017-04-01T20:06:44Z"
},
{
"checksumSHA1": "TkXI1L27/2icLlcE/4wTZCfxLeM=",
Expand Down