/block/partitions/

abot/go_modules/google.golang.org/api-0.182.0'>dependabot/go_modules/google.golang.org/api-0.182.0 Mirror of https://github.com/gohugoio/hugomatthias
summaryrefslogtreecommitdiffstats
path: root/tpl/template_i18n_test.go
blob: 2940c8d4d31a44cf860635ce421f8d1d6d1c7b09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2015 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tpl

import (
	"testing"

	"github.com/nicksnyder/go-i18n/i18n/bundle"
	"github.com/spf13/viper"
	"github.com/stretchr/testify/assert"
)

type test struct {
	file    string
	content []byte
}

func doTestI18nTranslate(t *testing.T, data []test, lang, id string) string {
	i18nBundle := bundle.New()

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

	SetTranslateLang(lang)

	translated, err := I18nTranslate(id, nil)
	if err != nil {
		t.Errorf("Error translating '%s': %s", id, err)
	}
	return translated
}

func TestI18nTranslate(t *testing.T) {
	var data []test
	var actual, expected string

	viper.SetDefault("DefaultContentLanguage", "en")

	// Test without and with placeholders
	for _, enablePlaceholders := range []bool{false, true} {
		viper.Set("EnableMissingTranslationPlaceholders", enablePlaceholders)

		// All translations present
		data = []test{
			{"en.yaml", []byte("- id: \"hello\"\n  translation: \"Hello, World!\"")},
			{"es.yaml", []byte("- id: \"hello\"\n  translation: \"¡Hola, Mundo!\"")},
		}
		expected = "¡Hola, Mundo!"
		actual = doTestI18nTranslate(t, data, "es", "hello")
		assert.Equal(t, expected, actual)

		// Translation missing in current language but present in default
		data = []test{
			{"en.yaml", []byte("- id: \"hello\"\n  translation: \"Hello, World!\"")},
			{"es.yaml", []byte("- id: \"goodbye\"\n  translation: \"¡Adiós, Mundo!\"")},
		}
		if enablePlaceholders {
			expected = "[i18n] hello"
		} else {
			expected = "Hello, World!"
		}
		actual = doTestI18nTranslate(t, data, "es", "hello")
		assert.Equal(t, expected, actual)

		// Translation missing in default language but present in current
		data = []test{
			{"en.yaml", []byte("- id: \"goodbye\"\n  translation: \"Goodbye, World!\"")},
			{"es.yaml", []byte("- id: \"hello\"\n  translation: \"¡Hola, Mundo!\"")},
		}
		expected = "¡Hola, Mundo!"
		actual = doTestI18nTranslate(t, data, "es", "hello")
		assert.Equal(t, expected, actual)

		// Translation missing in both default and current language
		data = []test{
			{"en.yaml", []byte("- id: \"goodbye\"\n  translation: \"Goodbye, World!\"")},
			{"es.yaml", []byte("- id: \"goodbye\"\n  translation: \"¡Adiós, Mundo!\"")},
		}
		if enablePlaceholders {
			expected = "[i18n] hello"
		} else {
			expected = ""
		}
		actual = doTestI18nTranslate(t, data, "es", "hello")
		assert.Equal(t, expected, actual)

		// Default translation file missing or empty
		data = []test{
			{"en.yaml", []byte("")},
		}
		actual = doTestI18nTranslate(t, data, "es", "hello")
		if enablePlaceholders {
			expected = "[i18n] hello"
		} else {
			expected = ""
		}
		assert.Equal(t, expected, actual)
	}
}