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

i18n: Allow custom language codes #3971

Closed
Closed
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
21 changes: 21 additions & 0 deletions i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (

"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)
Expand Down Expand Up @@ -137,10 +139,29 @@ var i18nTests = []i18nTest{
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)
Expand Down
20 changes: 20 additions & 0 deletions i18n/translationProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ package i18n

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 {
Expand All @@ -34,6 +40,9 @@ func NewTranslationProvider() *TranslationProvider {

// 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)}
Expand All @@ -48,6 +57,17 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error {

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() {
err := i18nBundle.ParseTranslationFileBytes(r.LogicalName(), r.Bytes())
Expand Down