@@ -2,6 +2,7 @@ package i18n
22
33import (
44 "fmt"
5+ "regexp"
56 "strings"
67 "testing"
78 "time"
@@ -48,6 +49,14 @@ func TestResolve(t *testing.T) {
4849 {"empty header falls back to English" , "" , "" , "en" },
4950 {"garbage header falls back to English" , "not a real header ;;;" , "" , "en" },
5051 {"override wins over Accept-Language" , "en" , "es" , "es" },
52+ // The embed widget forwards a host page's lang attribute verbatim, and that is
53+ // usually a regional tag Calnode has no file for.
54+ {"regional override falls back to its primary language" , "en" , "de-DE" , "de" },
55+ {"regional override of an unsupported language is ignored" , "es" , "ja-JP" , "es" },
56+ // de-x-neutral is opt-in: selectable by code, never picked for a German browser.
57+ {"neutral German can be selected by the ?lang override" , "en" , "de-x-neutral" , "de-x-neutral" },
58+ {"plain de is unaffected by de-x-neutral" , "de" , "" , "de" },
59+ {"de-DE is unaffected by de-x-neutral" , "de-DE,de;q=0.9" , "" , "de" },
5160 {"unsupported override is ignored" , "es" , "ja" , "es" },
5261 // ja isn't supported, but es (an acceptable lower-preference language) is — falling
5362 // through to it beats giving up to the site default, per Accept-Language semantics.
@@ -467,3 +476,63 @@ func TestAllLocalesHaveMatchingFormatVerbs_allowsLegitimateReordering(t *testing
467476 })
468477 }
469478}
479+
480+ // TestOptInVariantIsNotOffered pins what "opt-in" means for a wording variant: it loads
481+ // and resolves by code, and it is absent from everything a visitor is offered. If it
482+ // leaked into `supported`, the switcher would list "Deutsch" twice.
483+ func TestOptInVariantIsNotOffered (t * testing.T ) {
484+ const code = "de-x-neutral"
485+ if Get (code ) == nil {
486+ t .Fatalf ("%s.json is not loaded" , code )
487+ }
488+ for _ , o := range SupportedLocales () {
489+ if o .Code == code {
490+ t .Errorf ("%s must not be offered in the language switcher" , code )
491+ }
492+ }
493+ seen := map [string ]string {}
494+ for _ , o := range SupportedLocales () {
495+ if prev , dup := seen [o .Name ]; dup {
496+ t .Errorf ("locales %q and %q share the switcher name %q" , prev , o .Code , o .Name )
497+ }
498+ seen [o .Name ] = o .Code
499+ }
500+ }
501+
502+ // germanAddress matches a German pronoun of address in either register: formal
503+ // Sie/Ihnen/Ihr-, informal du/dir/dich/dein-. Case matters for the formal set, which is
504+ // what tells "Sie" (you) from "sie" (they, the cookies). \b is ASCII-only in RE2, which is
505+ // enough here: none of these words starts or ends with an umlaut.
506+ var germanAddress = regexp .MustCompile (`\b(Sie|Ihnen|Ihr[a-z]*|[Dd]u|[Dd]ir|[Dd]ich|[Dd]ein[a-z]*)\b` )
507+
508+ // TestGermanNeutralVariant guards the one property de-x-neutral exists for: no string
509+ // addresses the booker as du or Sie. It also has to stay complete, because a key it
510+ // lacks falls back to English rather than to de.
511+ func TestGermanNeutralVariant (t * testing.T ) {
512+ de , neutral := Get ("de" ), Get ("de-x-neutral" )
513+ if de == nil || neutral == nil {
514+ t .Fatal ("de and de-x-neutral must both ship" )
515+ }
516+ for k := range de .strings {
517+ if _ , ok := neutral .strings [k ]; ! ok {
518+ t .Errorf ("de-x-neutral.json is missing %q, which de.json has; it would render in English" , k )
519+ }
520+ }
521+ for k , v := range neutral .strings {
522+ if m := germanAddress .FindString (v ); m != "" {
523+ t .Errorf ("de-x-neutral.json: %s addresses the booker (%q): %q" , k , m , v )
524+ }
525+ }
526+ // The guard has teeth: the formal catalogue it was derived from must trip it.
527+ if ! germanAddress .MatchString (de .strings ["select_day_hint" ]) {
528+ t .Errorf ("germanAddress no longer matches the formal de.json wording %q" , de .strings ["select_day_hint" ])
529+ }
530+ // Dates are data, not register: the variant must format exactly as de does.
531+ for k , v := range de .strings {
532+ if strings .HasPrefix (k , "dow_short_" ) || strings .HasPrefix (k , "month_short_" ) || k == "date_format" || k == "clock_format" {
533+ if neutral .strings [k ] != v {
534+ t .Errorf ("de-x-neutral.json: %s = %q, de.json has %q" , k , neutral .strings [k ], v )
535+ }
536+ }
537+ }
538+ }
0 commit comments