From d317f565116d749722ab277523374c1205e90328 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Fri, 31 Mar 2017 15:04:52 +0200 Subject: [PATCH 1/4] docs, examples: Use TOML for i18n config files Fix #3200 --- docs/content/content/multilingual.md | 23 +++++++++++------------ examples/multilingual/i18n/en.toml | 2 ++ examples/multilingual/i18n/en.yaml | 3 --- examples/multilingual/i18n/et.toml | 2 ++ examples/multilingual/i18n/et.yaml | 2 -- 5 files changed, 15 insertions(+), 17 deletions(-) create mode 100644 examples/multilingual/i18n/en.toml delete mode 100644 examples/multilingual/i18n/en.yaml create mode 100644 examples/multilingual/i18n/et.toml delete mode 100644 examples/multilingual/i18n/et.yaml diff --git a/docs/content/content/multilingual.md b/docs/content/content/multilingual.md index bb0857743ce..6dfb1d32d46 100644 --- a/docs/content/content/multilingual.md +++ b/docs/content/content/multilingual.md @@ -118,9 +118,9 @@ 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: @@ -128,11 +128,11 @@ 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`: @@ -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: diff --git a/examples/multilingual/i18n/en.toml b/examples/multilingual/i18n/en.toml new file mode 100644 index 00000000000..30893b41197 --- /dev/null +++ b/examples/multilingual/i18n/en.toml @@ -0,0 +1,2 @@ +[head_title] +other = "Multilingual" diff --git a/examples/multilingual/i18n/en.yaml b/examples/multilingual/i18n/en.yaml deleted file mode 100644 index 55528cfee33..00000000000 --- a/examples/multilingual/i18n/en.yaml +++ /dev/null @@ -1,3 +0,0 @@ -- id: head_title - translation: "Multilingual" - diff --git a/examples/multilingual/i18n/et.toml b/examples/multilingual/i18n/et.toml new file mode 100644 index 00000000000..a96203eff0c --- /dev/null +++ b/examples/multilingual/i18n/et.toml @@ -0,0 +1,2 @@ +[head_title] +other = "Mitmekeelne" diff --git a/examples/multilingual/i18n/et.yaml b/examples/multilingual/i18n/et.yaml deleted file mode 100644 index 3402619cd6a..00000000000 --- a/examples/multilingual/i18n/et.yaml +++ /dev/null @@ -1,2 +0,0 @@ -- id: head_title - translation: "Mitmekeelne" From 071be202caba5015e21a69220a7f1d1930aed36c Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Sat, 1 Apr 2017 22:34:59 +0200 Subject: [PATCH 2/4] vendor: Update go-i18n Fix #2577 --- vendor/vendor.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/vendor/vendor.json b/vendor/vendor.json index 595c04f9878..3daed27aab4 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -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=", From 4e1c79eaa07d37a4f392f07b31cec8842bab72c3 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Sat, 1 Apr 2017 22:36:07 +0200 Subject: [PATCH 3/4] hugolib, i18n: Update tests with flat format and TOML files --- hugolib/hugo_sites_build_test.go | 8 ++++---- i18n/i18n_test.go | 35 +++++++++++++++----------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go index b5eac857c9a..80f4d5cc48e 100644 --- a/hugolib/hugo_sites_build_test.go +++ b/hugolib/hugo_sites_build_test.go @@ -1050,8 +1050,8 @@ 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) @@ -1059,8 +1059,8 @@ func createMultiTestSitesForConfig(t *testing.T, siteConfig testSiteConfig, conf 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) diff --git a/i18n/i18n_test.go b/i18n/i18n_test.go index fd9c91b3ec1..157c478d818 100644 --- a/i18n/i18n_test.go +++ b/i18n/i18n_test.go @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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 @@ -114,10 +114,10 @@ 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) @@ -125,11 +125,8 @@ func doTestI18nTranslate(t *testing.T, data map[string][]byte, lang, id string, } 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 } @@ -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) } } From 6a30b3501748ad162ed393417d3015dc96f5e322 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Sun, 2 Apr 2017 14:50:13 +0200 Subject: [PATCH 4/4] hugolib: Don't ignore errors from applyDepsIfNeeded --- hugolib/hugo_sites.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 6c737f65af2..2682e524dfb 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -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