Skip to content

Commit 578e4e1

Browse files
authored
Merge pull request #470 from labd/464-error-when-using-es-419-language-code-on-localized_value-does-not-allow-numbers
feat(localization): Added more support for IETF BCP 47 language tags
2 parents c68ac4c + 80ba682 commit 578e4e1

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: Added more support for IETF BCP 47 language tags
3+
time: 2024-01-19T11:16:34.573242447+01:00

commercetools/utils.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package commercetools
22

33
import (
44
"fmt"
5+
"github.com/hashicorp/go-cty/cty"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
57
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
6-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
78
"github.com/labd/commercetools-go-sdk/platform"
9+
"golang.org/x/text/language"
810
"reflect"
9-
"regexp"
1011
)
1112

1213
// TypeLocalizedString defined merely for documentation,
@@ -321,10 +322,35 @@ func nilIfEmpty(val *string) *string {
321322
return val
322323
}
323324

324-
var validateLocalizedStringKey = validation.MapKeyMatch(
325-
regexp.MustCompile("^[a-z]{2}(-[A-Z]{2})?$"),
326-
"Locale keys must match pattern ^[a-z]{2}(-[A-Z]{2})?$",
327-
)
325+
var validateLocalizedStringKey schema.SchemaValidateDiagFunc = func(v interface{}, path cty.Path) diag.Diagnostics {
326+
var diags diag.Diagnostics
327+
328+
m, ok := v.(map[string]interface{})
329+
if !ok {
330+
diags = append(diags, diag.Diagnostic{
331+
Severity: diag.Error,
332+
Summary: "Input is not a map of language tags",
333+
Detail: "The input provided is not a map of language tags",
334+
AttributePath: path,
335+
})
336+
return diags
337+
}
338+
339+
for key := range m {
340+
_, err := language.Parse(key)
341+
if err != nil {
342+
diags = append(diags, diag.Diagnostic{
343+
Severity: diag.Error,
344+
Summary: "Bad language tag",
345+
Detail: fmt.Sprintf("Language tag %s is not valid: %s", key, err.Error()),
346+
AttributePath: append(path, cty.IndexStep{Key: cty.StringVal(key)}),
347+
})
348+
continue
349+
}
350+
}
351+
352+
return diags
353+
}
328354

329355
func compareDateString(a, b string) bool {
330356
if a == b {

commercetools/utils_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestCompareDateString(t *testing.T) {
5353
for _, tt := range testCases {
5454
res = compareDateString(tt.a, tt.b)
5555
if res != tt.expected {
56-
t.Errorf("expected %v, got %v", tt.expected, res)
56+
t.Errorf("failures %v, got %v", tt.expected, res)
5757
}
5858
}
5959

@@ -92,3 +92,25 @@ func TestIntNilIfEmpty(t *testing.T) {
9292
assert.Equal(t, tt.expected, v)
9393
}
9494
}
95+
96+
func TestValidateLocalizedStringKey(t *testing.T) {
97+
testCases := []struct {
98+
input interface{}
99+
failures int
100+
}{
101+
{map[string]any{"en": "English"}, 0},
102+
{map[string]any{"en-US": "English (United States)"}, 0},
103+
{map[string]any{"es-419": "Spanish (Latin America)"}, 0},
104+
{map[string]any{"rm-sursilv": "Romansh Sursilvan"}, 0},
105+
{map[string]any{"sr-Cyrl": "Serbian in Cyrillic"}, 0},
106+
{map[string]any{"foobar": "Fail"}, 1},
107+
{"foobar", 1},
108+
{1, 1},
109+
{map[string]any{"es-409": "Spanish (Latin America)"}, 1},
110+
}
111+
112+
for _, tt := range testCases {
113+
diag := validateLocalizedStringKey(tt.input, nil)
114+
assert.Equal(t, tt.failures, len(diag), fmt.Sprintf("%+v", diag))
115+
}
116+
}

0 commit comments

Comments
 (0)