next-translate
next-translate copied to clipboard
Displays namespace:key if translation key is empty
Hello!
When key empty, like "key": ""
, translate function returns "(namespace):key
".
When key contains translation or space, it works like expected.
Shouldn't it return empty string instead of namespace:key when key is empty string?
@XMadFoX The namespace:key
is returned to make it easier to identify them. Maybe it would be nice if this was configurable. For now, the fallback can be configured at the translation level, but not at the global level.
t('common:invented', {}, { fallback: '' })
docs: https://github.com/vinissimus/next-translate/tree/1.0.1#8-fallbacks
I note it to version 1.1. Thank you for the proposal 😊
+1 on this one, it is ok (for us) to return "ns:key" if the key doesn't exist in the locale file, but not if I explicitely defined the translation to be empty with "key": ""
in the locale.json
Hello @aralroca,
do you think it would be sufficient to simply change the current behavior (meaning: interpret it as a bug that can be fixed without a new major version)? Or do you think this requires a new config option "allowEmptyTranslation" to be introduced?
If no option is required I could prepare a PR for the following diff:
--- a/src/transCore.tsx
+++ b/src/transCore.tsx
@@ -32,6 +32,11 @@ export default function transCore({ config, allNamespaces, pluralRules }) {
}
}
+ // no need to try interpolation
+ if (empty) {
+ return k
+ }
+
if (value instanceof Object) {
return objectInterpolation({
obj: value as Record<string, unknown>,
@@ -40,7 +45,9 @@ export default function transCore({ config, allNamespaces, pluralRules }) {
})
}
- return interpolation({ text: value as string, query, config }) || k
+ // this can return an empty string if either value was already empty
+ // or it contained only an interpolation (e.g. "{{name}}") and the query param was empty
+ return interpolation({ text: value as string, query, config })
}
return t
@@ -63,7 +70,10 @@ function getDicValue(
return {}
}
- return val[key as keyof typeof val] || {}
+ const res = val[key as keyof typeof val]
+
+ // pass all truthy values or (empty) strings
+ return res || (typeof res === "string" ? res : {})
}, dic)
if (
This would also fix another bug currently existing:
Using a translation like key: "{{something}}"
with a TranslationQuery of { something: '' }
results in ns:key
instead of the empty string.
@XMadFoX I prereleased 1.0.2-canary.4 with the PR that @j-schumann did if you want to start using it