Skip to content

Commit

Permalink
Clean translation usage
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurswag committed Oct 17, 2024
1 parent 304e719 commit e7931ed
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 87 deletions.
7 changes: 3 additions & 4 deletions frontend/src/lib/components/Forms/AutocompleteSelect.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import { formFieldProxy } from 'sveltekit-superforms';
import { toCamelCase } from '$lib/utils/locales';
import { safeTranslate } from '$lib/utils/i18n';
import { unsafeTranslate } from '$lib/utils/i18n';
import type { CacheLock } from '$lib/utils/types';
import { onMount } from 'svelte';
Expand Down Expand Up @@ -121,8 +120,8 @@
{#if option.suggested}
<span class="text-indigo-600">{option.label}</span>
<span class="text-sm text-gray-500"> (suggested)</span>
{:else if translateOptions && safeTranslate(toCamelCase(option.label))}
{safeTranslate(toCamelCase(option.label))}
{:else if translateOptions && unsafeTranslate(option.label)}
{unsafeTranslate(option.label)}
{:else}
{option.label}
{/if}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{#if value}
{m.missingMandatoyObjects1({ model: $modalStore[0].body })}:
{#each value as key}
<li class="font-bold">{safeTranslate(toCamelCase(key))}</li>
<li class="font-bold">{safeTranslate(key)}</li>
{/each}
{m.missingMandatoyObjects2()}.
{/if}
Expand Down
12 changes: 2 additions & 10 deletions frontend/src/lib/components/ModelTable/ModelTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,12 @@
{/if}
{:else if value && value.hexcolor}
<p class="flex w-fit min-w-24 justify-center px-2 py-1 rounded-md ml-2 whitespace-nowrap {classesHexBackgroundText(value.hexcolor)}" style="background-color: {value.hexcolor}">
{#if unsafeTranslate(toCamelCase(value.name ?? value.str ?? '-'))}
{unsafeTranslate(toCamelCase(value.name ?? value.str ?? '-'))}
{:else}
{value.name ?? value.str ?? '-'}
{/if}
{safeTranslate(value.name ?? value.str ?? '-')}
</p>
{:else if ISO_8601_REGEX.test(value)}
{formatDateOrDateTime(value, languageTag())}
{:else}
{#if unsafeTranslate(toCamelCase(value))}
{unsafeTranslate(toCamelCase(value))}
{:else}
{value ?? '-'}
{/if}
{unsafeTranslate(value) ?? '-'}
{/if}
</span>
{/if}
Expand Down
20 changes: 6 additions & 14 deletions frontend/src/lib/utils/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,24 @@ import * as m from '$paraglide/messages.js';
import { toCamelCase } from '$lib/utils/locales';

/**
* Safe translate function that returns the key if the translation is not found.
* Unafe translate function that doesn't return anything if the key if the translation is not found.
* @param key The key to translate.
* @param params The parameters to pass to the translation function.
* @param options The options to pass to the translation function.
*/
export function safeTranslate(key: string, params = {}, options = {}): string {
if (Object.hasOwn(m, toCamelCase(key))) {
return m[toCamelCase(key)](params, options);
}
export function unsafeTranslate(key: string, params = {}, options = {}): string | undefined {
key = toCamelCase(key);
if (Object.hasOwn(m, key)) {
return m[key](params, options);
}
return key;
}

/**
* Unafe translate function that doesn't return anything if the key if the translation is not found.
* Safe translate function that returns the key if the translation is not found.
* @param key The key to translate.
* @param params The parameters to pass to the translation function.
* @param options The options to pass to the translation function.
*/
export function unsafeTranslate(key: string, params = {}, options = {}): string {
if (Object.hasOwn(m, toCamelCase(key))) {
return m[toCamelCase(key)](params, options);
}
if (Object.hasOwn(m, key)) {
return m[key](params, options);
}
export function safeTranslate(key: string, params = {}, options = {}): string {
return unsafeTranslate(key) || key;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const actions: Actions = {
return message(
rejectForm,
m.successfullyRejectedObject({
object: safeTranslate(toCamelCase(model.toLowerCase())).toLowerCase(),
object: safeTranslate(model).toLowerCase(),
id: id
})
);
Expand Down Expand Up @@ -88,7 +88,7 @@ export const actions: Actions = {
return message(
acceptForm,
m.successfullyValidatedObject({
object: safeTranslate(toCamelCase(model.toLowerCase())).toLowerCase(),
object: safeTranslate(model).toLowerCase(),
id: id
})
);
Expand Down Expand Up @@ -122,7 +122,7 @@ export const actions: Actions = {
return message(
revokeForm,
m.successfullyRevokedObject({
object: safeTranslate(toCamelCase(model.toLowerCase())).toLowerCase(),
object: safeTranslate(model).toLowerCase(),
id: id
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
{#if key === 'urn'}
{m.urn()}
{:else}
{safeTranslate(toCamelCase(key))}
{safeTranslate(key)}
{/if}
</div>
<ul class="text-sm">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
<span class="font-semibold">{m.status()}:</span>
{risk_assessment.status === null
? '--'
: safeTranslate(toCamelCase(risk_assessment.status))}
: safeTranslate(risk_assessment.status)}
</li>
<li class="pb-1">
<span class="font-semibold">{m.authors()}:</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<div>
<p class="text-sm font-semibold text-gray-400">{m.treatmentStatus()}</p>
<p class="text-sm font-semibold">
{safeTranslate(toCamelCase(data.scenario.treatment))}
{safeTranslate(data.scenario.treatment)}
</p>
</div>
</div>
Expand Down Expand Up @@ -135,11 +135,7 @@
class="text-sm text-center font-semibold p-2 rounded-md w-20"
style="background-color: {color_map[data.scenario.current_proba]}"
>
{#if safeTranslate(toCamelCase(data.scenario.current_proba.name))}
{safeTranslate(toCamelCase(data.scenario.current_proba.name))}
{:else}
{data.scenario.current_proba.name}
{/if}
{safeTranslate(data.scenario.current_proba.name)}
</span>
</p>
<i class="fa-solid fa-xmark mt-5" />
Expand All @@ -149,11 +145,7 @@
class="text-sm text-center font-semibold p-2 rounded-md w-20"
style="background-color: {color_map[data.scenario.current_impact]}"
>
{#if safeTranslate(toCamelCase(data.scenario.current_impact.name))}
{safeTranslate(toCamelCase(data.scenario.current_impact.name))}
{:else}
{data.scenario.current_impact.name}
{/if}
{safeTranslate(data.scenario.current_impact.name)}
</span>
</p>
<i class="fa-solid fa-equals mt-5" />
Expand All @@ -167,11 +159,7 @@
)}"
style="background-color: {data.scenario.current_level.hexcolor}"
>
{#if safeTranslate(toCamelCase(data.scenario.current_level.name))}
{safeTranslate(toCamelCase(data.scenario.current_level.name))}
{:else}
{data.scenario.current_level.name}
{/if}
{safeTranslate(data.scenario.current_level.name)}
</span>
</p>
</div>
Expand All @@ -193,11 +181,7 @@
class="text-sm text-center font-semibold p-2 rounded-md w-20"
style="background-color: {color_map[data.scenario.residual_proba]}"
>
{#if safeTranslate(toCamelCase(data.scenario.residual_proba.name))}
{safeTranslate(toCamelCase(data.scenario.residual_proba.name))}
{:else}
{data.scenario.residual_proba.name}
{/if}
{safeTranslate(data.scenario.residual_proba.name)}
</span>
</p>
<i class="fa-solid fa-xmark mt-5" />
Expand All @@ -207,11 +191,7 @@
class="text-sm text-center font-semibold p-2 rounded-md w-20"
style="background-color: {color_map[data.scenario.residual_impact]}"
>
{#if safeTranslate(toCamelCase(data.scenario.residual_impact.name))}
{safeTranslate(toCamelCase(data.scenario.residual_impact.name))}
{:else}
{data.scenario.residual_impact.name}
{/if}
{safeTranslate(data.scenario.residual_impact.name)}
</span>
</p>
<i class="fa-solid fa-equals mt-5" />
Expand All @@ -225,11 +205,7 @@
)}"
style="background-color: {data.scenario.residual_level.hexcolor}"
>
{#if safeTranslate(toCamelCase(data.scenario.residual_level.name))}
{safeTranslate(toCamelCase(data.scenario.residual_level.name))}
{:else}
{data.scenario.residual_level.name}
{/if}
{safeTranslate(data.scenario.residual_level.name)}
</span>
</p>
</div>
Expand All @@ -241,9 +217,8 @@
<span class="font-semibold">
{#each data.scenario.qualifications as qualification, i}
{#if i > 0},{/if}
{safeTranslate(toCamelCase(qualification)) ||
qualification ||
safeTranslate('undefined')}
{safeTranslate(qualification) ||
m.undefined()}
{/each}
</span>
</p>
Expand All @@ -255,11 +230,7 @@
{data.scenario.strength_of_knowledge.symbol}
{/if}
<span class="font-semibold">
{#if safeTranslate(toCamelCase(data.scenario.strength_of_knowledge.name))}
{safeTranslate(toCamelCase(data.scenario.strength_of_knowledge.name))}
{:else}
{safeTranslate('undefined')}
{/if}
{safeTranslate(data.scenario.strength_of_knowledge.name) || m.undefined()}
</span>
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@
)}"
style="background-color: {riskLevel.hexcolor}"
>
{#if safeTranslate(toCamelCase(riskLevel.name))}
{safeTranslate(toCamelCase(riskLevel.name))}
{:else}
{riskLevel.name}
{/if}
{safeTranslate(riskLevel.name)}
</div>
{:else}
<div class="flex font-medium w-32 justify-center p-2 rounded-token bg-gray-300">--</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@
class="text-sm font-medium text-gray-800 capitalize-first"
data-testid={key.replaceAll('_', '-') + '-field-title'}
>
{#if key === 'urn'}
{m.urn()}
{:else}
{safeTranslate(toCamelCase(key))}
{/if}
{safeTranslate(key)}
</div>
<ul class="text-sm">
<li
Expand Down Expand Up @@ -244,10 +240,8 @@
{:else}
{value.str}
{/if}
{:else if safeTranslate(toCamelCase(value.str ?? value))}
{safeTranslate(toCamelCase(value.str ?? value))}
{:else}
{value.str ?? value}
{safeTranslate(value.str ?? value)}
{/if}
{:else}
--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
class="text-sm font-medium text-gray-800 capitalize-first"
data-testid={key.replace('_', '-') + '-field-title'}
>
{safeTranslate(toCamelCase(key))}
{safeTranslate(key)}
</div>
<ul class="text-sm">
<li
Expand Down

0 comments on commit e7931ed

Please sign in to comment.