Skip to content

Commit d3eb562

Browse files
author
Sebi Nemeth
committed
handle multipart
1 parent 02859f0 commit d3eb562

File tree

6 files changed

+61
-27
lines changed

6 files changed

+61
-27
lines changed

dist/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type TranslationMeta = {
22
locale: string;
33
message: string;
44
values?: object;
5+
choice?: number;
56
path: string;
67
};
78
type LiveTranslatorPluginOptions = {

dist/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@ export function encodeMessages(messagesObject) {
7575
const messages = cloneDeep(messagesObject);
7676
forIn(messages, (localeMessages, locale) => {
7777
deepForIn(localeMessages, (message, path) => {
78-
const meta = ZeroWidthEncoder.encode(JSON.stringify({
79-
locale,
80-
message,
81-
path,
82-
}));
83-
set(localeMessages, path, meta + message);
78+
const parts = message.split('|').map(part => part.trim());
79+
for (let i = 0; i < parts.length; i++) {
80+
const meta = ZeroWidthEncoder.encode(JSON.stringify({
81+
locale,
82+
message,
83+
path,
84+
choice: i || undefined,
85+
}));
86+
parts[i] = meta + parts[i];
87+
}
88+
set(localeMessages, path, parts.join(' | '));
8489
});
8590
});
8691
return messages;

src/demo/App.vue

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,35 @@
1414
<br>
1515
Live Translator Plugin
1616
<br>
17-
<small>demo application</small>
17+
<small>demo app</small>
1818
</h1>
19+
<p>
20+
This is a demo app and development test page for the plugin. To turn the plugin on/off, click the <b>LT</b> button
21+
in the top left corner. Localized strings appear in <span class="translated">red</span>.
22+
</p>
1923
<div class="selector">
2024
<button :class="{ active: locale === 'en' }" @click="locale = 'en'">English</button>
2125
<button :class="{ active: locale === 'hu' }" @click="locale = 'hu'">Hungarian</button>
2226
</div>
2327
<h3>Simple string</h3>
24-
<p>{{ $t('LTPlugin.HelloWorld') }}</p>
28+
<p class="translated">{{ $t('LTPlugin.HelloWorld') }}</p>
2529
<h3>Interplolation</h3>
26-
<p>{{ $t('LTPlugin.WelcomeToLT', { app: 'Live Translator Plugin'}) }}</p>
30+
<p class="translated">{{ $t('LTPlugin.WelcomeToLT', { app: 'Live Translator Plugin' }) }}</p>
2731
<h3>Plurals</h3>
2832
<input class="plural" type="number" min="0" step="1" v-model="pluralCount">
29-
<br>
30-
{{ $t('LTPlugin.Messages', pluralCount) }}
31-
<br>
32-
{{ $t('LTPlugin.MessagesCount', pluralCount) }}
33+
<p class="translated">
34+
{{ $t('LTPlugin.Messages', pluralCount) }}
35+
</p>
36+
<p class="translated">
37+
{{ $t('LTPlugin.MessagesCount', pluralCount) }}
38+
</p>
39+
<h3>Multiple strings inside one tag</h3>
40+
<p class="translated">
41+
{{ t('LTPlugin.PartOne') }} {{ $t('LTPlugin.PartTwo') }}
42+
</p>
3343
<h3>Attribute</h3>
34-
<img class="image" src="https://source.unsplash.com/random/500x500" :alt="t('LTPlugin.Attrs.ImageAlt')" :title="t('LTPlugin.Attrs.ImageTitle')">
44+
<img class="image" src="https://source.unsplash.com/random/500x500" :alt="t('LTPlugin.Attrs.ImageAlt')"
45+
:title="t('LTPlugin.Attrs.ImageTitle')">
3546
</div>
3647
</div>
3748
</template>
@@ -44,19 +55,26 @@ const { t, locale } = useI18n()
4455
const pluralCount = ref(1)
4556
const showMeta = computed(() => {
4657
const params = new URLSearchParams(location.search)
47-
if (params.get('meta')) {
48-
return JSON.parse(decodeURIComponent(params.get('meta')))
58+
const meta = params.get('meta')
59+
if (meta) {
60+
return JSON.parse(decodeURIComponent(meta))
4961
}
5062
return null
5163
})
5264
5365
</script>
5466

5567
<style lang="scss" scoped>
68+
.translated {
69+
color: rgb(161, 0, 0);
70+
}
71+
5672
.show-meta {
5773
table {
5874
text-align: left;
59-
th, td {
75+
76+
th,
77+
td {
6078
padding: 4px 8px;
6179
}
6280
}

src/demo/lang/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"Attrs": {
88
"ImageAlt": "Image alt",
99
"ImageTitle": "Image title"
10-
}
10+
},
11+
"PartOne": "Part one",
12+
"PartTwo": "Part two"
1113
}
1214
}

src/demo/lang/hu.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"Attrs": {
88
"ImageAlt": "Képleírás",
99
"ImageTitle": "Képcím"
10-
}
10+
},
11+
"PartOne": "Első rész",
12+
"PartTwo": "Második rész"
1113
}
1214
}

src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export type TranslationMeta = {
6565
locale: string,
6666
message: string,
6767
values?: object,
68+
choice?: number,
6869
path: string,
6970
}
7071

@@ -88,14 +89,19 @@ export function encodeMessages(messagesObject) {
8889
const messages = cloneDeep(messagesObject)
8990
forIn(messages, (localeMessages, locale) => {
9091
deepForIn(localeMessages, (message, path) => {
91-
const meta = ZeroWidthEncoder.encode(
92-
JSON.stringify({
93-
locale,
94-
message,
95-
path,
96-
} as TranslationMeta),
97-
)
98-
set(localeMessages, path, meta+message)
92+
const parts = message.split('|').map(part => part.trim())
93+
for (let i = 0; i < parts.length; i++) {
94+
const meta = ZeroWidthEncoder.encode(
95+
JSON.stringify({
96+
locale,
97+
message,
98+
path,
99+
choice: i || undefined,
100+
} as TranslationMeta),
101+
)
102+
parts[i] = meta + parts[i]
103+
}
104+
set(localeMessages, path, parts.join(' | '))
99105
})
100106
})
101107
return messages

0 commit comments

Comments
 (0)