Skip to content

Commit fc6f009

Browse files
authored
Fall back to default locale when key doesn't exist for requested locale in string translation (#78)
1 parent 4b5bf5a commit fc6f009

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

pkg/code/localization/keys.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,23 @@ func localizeKey(locale language.Tag, key string, args ...string) (string, *lang
138138
return "", nil, errors.New("localization bundle not configured")
139139
}
140140

141-
localizer := i18n.NewLocalizer(bundle, locale.String())
142-
143141
localizeConfigInvite := i18n.LocalizeConfig{
144142
MessageID: key,
145143
}
146144

145+
localizer := i18n.NewLocalizer(bundle, locale.String())
147146
localized, tag, err := localizer.LocalizeWithTag(&localizeConfigInvite)
148-
if err != nil {
147+
switch err.(type) {
148+
case *i18n.MessageNotFoundErr:
149+
// Fall back to default locale if the key doesn't exist for the requested
150+
// locale
151+
localizer := i18n.NewLocalizer(bundle, defaultLocale.String())
152+
localized, tag, err = localizer.LocalizeWithTag(&localizeConfigInvite)
153+
if err != nil {
154+
return "", nil, err
155+
}
156+
case nil:
157+
default:
149158
return "", nil, err
150159
}
151160

0 commit comments

Comments
 (0)