Skip to content

Commit c8821de

Browse files
authored
Merge pull request #1 from apicore-engineering/develop
Fix circular dependency issue
2 parents c561503 + 834f039 commit c8821de

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

dist/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import VueI18n from 'vue-i18n';
22
export type TranslationMeta = {
33
locale: string;
44
message: string;
5-
values: unknown;
5+
values?: object;
66
path: string;
77
};
88
type LiveTranslatorPluginOptions = {

dist/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,19 @@ class LiveTranslatorManager {
146146
const original = originalFormatter.interpolate(message, values, path);
147147
let meta = '';
148148
try {
149+
// filter nested objects, replace inner objects with string 'object'
150+
// this is needed when values from <i18n> tags are circular dependent objects
151+
const filteredValues = Object.fromEntries(Object.entries(values || {})
152+
.map(([key, value]) => [key, typeof value !== 'object' ? value : 'object']));
149153
meta = ZeroWidthEncoder.encode(JSON.stringify({
150154
message,
151-
values,
155+
values: filteredValues,
152156
path,
153157
locale: self._options.i18n.locale,
154158
}));
155159
}
156160
catch (exception) {
157-
console.warn(path, exception);
161+
console.warn(message, values, path, self._options.i18n.locale, exception);
158162
}
159163
return (original && meta && self._enabled) ? [meta, ...original] : original;
160164
},

src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const css = `
6262
export type TranslationMeta = {
6363
locale: string,
6464
message: string,
65-
values: unknown,
65+
values?: object,
6666
path: string,
6767
}
6868

@@ -167,20 +167,26 @@ class LiveTranslatorManager {
167167
const originalFormatter = this._options.i18n.formatter
168168
const self = this
169169
this._options.i18n.formatter = {
170-
interpolate (message, values, path) {
170+
interpolate (message: string, values: object | null, path: string) {
171171
const original = originalFormatter.interpolate(message, values, path) as unknown[] | null
172172
let meta = ''
173173
try {
174+
// filter nested objects, replace inner objects with string 'object'
175+
// this is needed when values from <i18n> tags are circular dependent objects
176+
const filteredValues = Object.fromEntries(
177+
Object.entries(values || {})
178+
.map(([key, value]) => [key, typeof value !== 'object' ? value : 'object'])
179+
)
174180
meta = ZeroWidthEncoder.encode(
175181
JSON.stringify({
176182
message,
177-
values,
183+
values: filteredValues,
178184
path,
179185
locale: self._options.i18n.locale,
180-
}),
186+
} as TranslationMeta),
181187
)
182188
} catch (exception) {
183-
console.warn(path, exception)
189+
console.warn(message, values, path, self._options.i18n.locale, exception)
184190
}
185191

186192
return (original && meta && self._enabled) ? [meta, ...original] : original

0 commit comments

Comments
 (0)