shithub: hugo

Download patch

ref: bb04010cbfd92837685f98c3e0f5362ba00ce487
parent: 76dc811c6539b2ed8b4d3b22693e5088b9f6ecfe
author: Kevin Gimbel <[email protected]>
date: Sun Oct 15 08:35:25 EDT 2017

i18n: Allow custom language codes

Use the new `RegisterPluralSpec` function to register all defined
languages. This allows the usage of language identifiers which are not
part of the Unicode CLDR standard.

Closes #3564

--- a/i18n/i18n_test.go
+++ b/i18n/i18n_test.go
@@ -23,9 +23,11 @@
 
 	"github.com/gohugoio/hugo/config"
 	"github.com/nicksnyder/go-i18n/i18n/bundle"
+	"github.com/nicksnyder/go-i18n/i18n/language"
 	jww "github.com/spf13/jwalterweatherman"
 	"github.com/spf13/viper"
 	"github.com/stretchr/testify/require"
+	"strings"
 )
 
 var logger = jww.NewNotepad(jww.LevelError, jww.LevelError, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
@@ -137,10 +139,29 @@
 		expected:     "hello",
 		expectedFlag: "[i18n] hello",
 	},
+	// Non Unicode CLDR language code
+	{
+		data: map[string][]byte{
+			"dk.toml": []byte("[hello]\nother = \"hej\""),
+		},
+		args:         nil,
+		lang:         "dk",
+		id:           "hello",
+		expected:     "hej",
+		expectedFlag: "hej",
+	},
 }
 
 func doTestI18nTranslate(t *testing.T, test i18nTest, cfg config.Provider) string {
 	i18nBundle := bundle.New()
+	ids := []string{}
+
+	for file := range test.data {
+		id := strings.TrimSuffix(file, ".toml")
+		ids = append(ids, id)
+	}
+
+	language.RegisterPluralSpec(ids, &language.PluralSpec{})
 
 	for file, content := range test.data {
 		err := i18nBundle.ParseTranslationFileBytes(file, content)
--- a/i18n/translationProvider.go
+++ b/i18n/translationProvider.go
@@ -15,12 +15,18 @@
 
 import (
 	"fmt"
+	"sync"
 
 	"github.com/gohugoio/hugo/deps"
 	"github.com/gohugoio/hugo/source"
 	"github.com/nicksnyder/go-i18n/i18n/bundle"
+	"github.com/nicksnyder/go-i18n/i18n/language"
 )
 
+// Unfortunately this needs to be global, see
+// https://github.com/nicksnyder/go-i18n/issues/82
+var tpMu sync.Mutex
+
 // TranslationProvider provides translation handling, i.e. loading
 // of bundles etc.
 type TranslationProvider struct {
@@ -34,6 +40,9 @@
 
 // Update updates the i18n func in the provided Deps.
 func (tp *TranslationProvider) Update(d *deps.Deps) error {
+	tpMu.Lock()
+	defer tpMu.Unlock()
+
 	dir := d.PathSpec.AbsPathify(d.Cfg.GetString("i18nDir"))
 	sp := source.NewSourceSpec(d.Cfg, d.Fs)
 	sources := []source.Input{sp.NewFilesystem(dir)}
@@ -47,6 +56,17 @@
 	d.Log.DEBUG.Printf("Load I18n from %q", sources)
 
 	i18nBundle := bundle.New()
+
+	langs := []string{}
+	for _, currentSource := range sources {
+		for _, r := range currentSource.Files() {
+			langs = append(langs, r.BaseFileName())
+		}
+	}
+	// We need to register all language codes as "plural spec" to prevent errors with unknown language codes.
+	// see https://github.com/gohugoio/hugo/issues/3564
+	ps := &language.PluralSpec{}
+	language.RegisterPluralSpec(langs, ps)
 
 	for _, currentSource := range sources {
 		for _, r := range currentSource.Files() {