Skip to content

Commit db02fba

Browse files
committed
fix: correctly parse nwp files
1 parent b0c7b9d commit db02fba

File tree

15 files changed

+5404
-20
lines changed

15 files changed

+5404
-20
lines changed

app/app.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
</UHeader>
2626

2727
<UMain>
28-
<NuxtPage />
28+
<UContainer>
29+
<NuxtPage />
30+
</UContainer>
2931
</UMain>
3032

3133
<USeparator icon="i-simple-icons-nuxtdotjs" />

app/components/TranslationFileForm.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ const state = reactive<Partial<Schema>>({
5555
translations: model.value,
5656
});
5757
58+
watch(model, (newVal) => {
59+
state.translations = newVal;
60+
});
61+
5862
const { showSuccess } = useFlash();
5963
6064
const handleSubmit = (event: FormSubmitEvent<Schema>) => {

app/pages/export.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<UPage class="px-4">
2+
<UPage>
33
<UPageHeader :title="title" :description="description" />
44
<UPageBody>
55
<UPageCard title="Veranderingen">

app/pages/import.vue

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
<template>
2-
<UPage class="px-4">
2+
<UPage>
33
<UPageHeader :title="title" :description="description" />
44
<UPageBody>
55
<UPageCard title="Vertaling">
66
<TranslationFileForm v-model="translationsString" required />
77
</UPageCard>
88
<UPageCard title="Origineel">
9+
<div class="flex gap-4">
10+
<UButton
11+
loading-auto
12+
class="w-fit"
13+
color="neutral"
14+
label="Vul automatisch in"
15+
@click="autoFillOriginals"
16+
/>
17+
<UButton
18+
class="w-fit"
19+
color="error"
20+
label="Reset"
21+
@click="originalsString = undefined"
22+
/>
23+
</div>
924
<TranslationFileForm
1025
v-model="originalsString"
1126
:field="{
@@ -34,4 +49,24 @@ const { originalsString, translationsString } = storeToRefs(importStore);
3449
watch(translationsString, (newVal) => {
3550
importStore.translations = parseTranslationFile(newVal);
3651
});
52+
53+
const { showError } = useFlash();
54+
const loadingOriginals = ref(false);
55+
56+
const autoFillOriginals = async () => {
57+
loadingOriginals.value = true;
58+
try {
59+
const strings = await $fetch<string>(
60+
"https://docs.google.com/feeds/download/documents/export/Export?exportFormat=txt&id=1KOm9MTLrWv_lll6f1YvnlWlq3srXYVKMSo2a9KZ39a8",
61+
{ responseType: "text" },
62+
);
63+
originalsString.value = strings.trim().replaceAll("\r", "");
64+
} catch {
65+
showError({
66+
description:
67+
"Er is een fout opgetreden bij het ophalen van de originele teksten.",
68+
});
69+
}
70+
loadingOriginals.value = false;
71+
};
3772
</script>

app/pages/translate.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<UPage class="px-4">
2+
<UPage>
33
<template #left>
44
<UPageAside>
55
<template #top>

app/pages/update.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<UPage class="px-4">
2+
<UPage>
33
<UPageHeader :title="title" :description="description" />
44

55
<UPageBody>

app/utils/nws.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ const normalizeTranslationLine = (
6363
): NWSTranslationLine => {
6464
if (isNWPTranslationLine(line)) {
6565
const key = line.match(/^"([^"]+)"/)?.[1];
66-
const value = line.match(/:"([^"]+)"/)?.[1];
67-
return `${key}: ${value}`;
66+
const value = line.match(/: "([^"]+)"/)?.[1];
67+
return `${key}: ${value || "<empty>"}`;
6868
}
6969

70-
return line;
70+
return (line.endsWith("\r") ? line.slice(0, -1) : line) as NWSTranslationLine;
7171
};
7272

7373
export const parseTranslationFile = (file: TranslationFile): ProgramUIFile => {
@@ -79,9 +79,13 @@ export const parseTranslationFile = (file: TranslationFile): ProgramUIFile => {
7979

8080
lines.forEach((line) => {
8181
const [key, ...rest] = line.trim().split(KEY_VALUE_SEPARATOR);
82-
const value = rest.join(KEY_VALUE_SEPARATOR);
8382

84-
if (key) programUI[key] = value ?? ""; // Value can be empty in NWP translation file
83+
// Some strings end with a colon and space (": "), keep it in the value
84+
const value = line.endsWith(KEY_VALUE_SEPARATOR)
85+
? rest.join(KEY_VALUE_SEPARATOR) + " "
86+
: rest.join(KEY_VALUE_SEPARATOR);
87+
88+
if (key && value) programUI[key] = value === "<empty>" ? "" : value; // Value can be empty in NWP translation file
8589
});
8690

8791
return programUI;
@@ -93,9 +97,9 @@ export const serializeTranslationFile = (
9397
const isNWP = isNWPProgramUIFile(record);
9498
return Object.entries(record)
9599
.map(
96-
([key, value]): TranslationLine =>
100+
([key, value], i): TranslationLine =>
97101
isNWP
98-
? `"${key}"${KEY_VALUE_SEPARATOR}"${value}",`
102+
? `"${key}"${KEY_VALUE_SEPARATOR}"${value}"${i < Object.keys(record).length - 1 ? "," : ""}`
99103
: `${key}: ${value}`,
100104
)
101105
.join(LINE_SEPARATOR);

app/utils/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const translationFileSchema = z.custom<TranslationFile>(
77
/^("?[a-zA-Z_\d]+"?: "?.*?"?,?\n)+("?[a-zA-Z_\d]+"?: "?.*?"?,?\n*)$/.test(
88
val,
99
),
10-
"Kan waarde niet parseren.",
10+
"Kan waarde niet valideren.",
1111
);
1212

1313
export const programUISchema = z.record(z.string(), z.string());

0 commit comments

Comments
 (0)