Skip to content

Commit

Permalink
i18n slots supports for custom interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
BobChao87 committed Oct 5, 2021
1 parent 5ed006a commit 52dff1d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions configs/vue-i18n.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@
import { dateTimeFormats, locales } from './i18n.config';

class WeblateFormatter {
tokenRegex = /^(?:{{([\w-]+)}}|{([\w-]+)})/;

interpolate(message, values) {
if (!values) {
return [ message ];
}
Object.entries(values).forEach(([ key, value ]) => {
const replacement = new RegExp(`{?{${key}}}?`, 'gi');
message = message.replace(replacement, value);
});
return [ message ];
const compiled = [ '' ];
let index = 0;
while (index < message.length) {
const character = message[index];
if (character === '{') {
const tokenMatch = this.tokenRegex.exec(message.slice(index));
if (tokenMatch) {
const token = tokenMatch[1] ?? tokenMatch[2];
if (Object.prototype.hasOwnProperty.call(values, token)) {
compiled.push(values[token], '');
index += tokenMatch[0].length;
continue;
}
}
}
compiled[compiled.length - 1] += character;
index++;
}
return compiled;
}
};

Expand Down

0 comments on commit 52dff1d

Please sign in to comment.