Repository navigation
Take the application's t function and expose one translate() - #1
Merged
Merged
Conversation
translateStrings keeps its callback and every entry of strings is a computed over it, so a callback that reads vue-i18n's locale updates every string on a locale switch. The callback also receives the string's placeholders, to pass to an interpolating lookup that would otherwise substitute them with nothing, and lookup() resolves keys that only exist at run time through the same callback.
✅ Incremental coverage — 100%
6 files not graded (2 ignored by pattern, 4 not source files)
Coverage reports: coverage/coverage-final.json (istanbul, 1 file) — incremental-coverage-check v3.0.0 |
3 tasks done
vue-i18n's tm() already returns the raw message, resolved through the fallback locale, so the callback needs no third argument to survive t()'s interpolation.
translateStrings(t, namespace) takes a function shaped like vue-i18n's t and calls it with the library's own params, so the i18n library substitutes the placeholders and a key it returns unchanged falls back to the English default. translate(key, params, defaultValue) replaces strings, lookup and the free translate() for declared and run-time keys alike.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
translateStrings(cb)calledcbonce for every key and stored the results, so a callback built on vue-i18n froze every string in the locale active at the call. An application that starts in the browser's locale and switches to the user's saved one keeps the library strings in the first locale.The callback also had to return the raw template for the library to interpolate, and vue-i18n has no reliable way to give one:
t()substitutes placeholders with empty strings when it is not given params, andtm()returns compiled functions instead of text once messages are compiled at build time.Keys that only exist at run time, such as error codes defined by an application's backend, could not be translated at all: the key set was fixed by
createTranslatable.Change
translateStrings(t, namespace?)takes the application's translation function, shaped like vue-i18n's and i18next'st:(key, named) => string, returning the key unchanged when there is no translation. The library calls it ast('forms.MinValue', { minValue: 5 })with its own values, so the i18n library does the interpolation and no raw template is needed. A key returned unchanged falls back to the library's English default, after the fallback localestitself consults.translate(key, params?, defaultValue?)is the one way a library reads a string, declared or not. It is reactive wherever it is read, astis: a template, a computed, a watcher. A run-time key falls back todefaultValue, then to the key.strings,lookup, the freetranslate(strings, key, params)andTranslateStringsCallback.strings.Xcannot survive the change: for a string with placeholders it would have to calltwithout params, which erases them.Version 0.2.0, since the public API changes. vue-forms, vuetify-inputs, vue-grid and fastapi-viewsets move to
translate(key, params)and re-export the newtranslateStringsin their own releases.Usage in an application
Test plan
npm test: 16 tests against a real vue-i18n 11 (dev dependency only): translation with params substituted byt, fallback locale, English default for a missing key, locale switch updating a computed and re-running an effect the way a render does, re-passing the same function over non-reactive translations, run-time keys with and without a default, 100% coveragenpm run lint:ci: vue-tsc acceptsi18n.global.tfromcreateI18nwith inferred message types as aTranslateFunctionnpm run build