Skip to content

Commit

Permalink
Merge pull request #868 from MuckRock/818-translation-strings
Browse files Browse the repository at this point in the history
Standardize on 'en', not 'en-US' as a language code, but register both
  • Loading branch information
eyeseast authored Nov 21, 2024
2 parents e303b02 + dd5672e commit fd77515
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ export const handleError = Sentry.handleErrorWithSentry();
/** @type {import('@sveltejs/kit').Handle} */
async function language({ event, resolve }) {
const lang =
event.request.headers.get("accept-language")?.split(",")[0] ?? "en-US";
event.request.headers.get("accept-language")?.split(",")[0] ?? "en";

if (lang) {
locale.set(lang);
// use en.json for en-US and such
const [language, ...tags] = lang.split("-");

if (language) {
locale.set(language);
}

return resolve(event, {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/documents/Access.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<script lang="ts">
import { _ } from "svelte-i18n";
import type { Access } from "@/lib/api/types";
import type { Access } from "$lib/api/types";
import type { Level } from "$lib/components/inputs/AccessLevel.svelte";
export let level: Level;
Expand Down
14 changes: 7 additions & 7 deletions src/lib/components/documents/BulkActions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ Most actual actions are deferred to their own forms, so this is more of a switch
const selected: Readable<Document[]> = getContext("selected");
const actions: Record<Action, ActionDetail> = {
share: [$_("bulk.actions.share"), Share16],
edit: [$_("bulk.actions.edit"), Pencil16],
data: [$_("bulk.actions.data"), Tag16],
project: [$_("bulk.actions.project"), FileDirectory16],
reprocess: [$_("bulk.actions.reprocess"), IssueReopened16],
delete: [$_("bulk.actions.delete"), Alert16],
share: ["bulk.actions.share", Share16],
edit: ["bulk.actions.edit", Pencil16],
data: ["bulk.actions.data", Tag16],
project: ["bulk.actions.project", FileDirectory16],
reprocess: ["bulk.actions.reprocess", IssueReopened16],
delete: ["bulk.actions.delete", Alert16],
};
let visible: Nullable<Action> = null;
Expand Down Expand Up @@ -76,7 +76,7 @@ Most actual actions are deferred to their own forms, so this is more of a switch
}}
>
<svelte:component this={icon} slot="start" />
{label}
{$_(label)}
</SidebarItem>
{/each}

Expand Down
9 changes: 8 additions & 1 deletion src/lib/i18n/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { browser } from "$app/environment";
import { init, register } from "svelte-i18n";
import { LANGUAGES } from "@/config/config.js";
import { getLanguage } from "../utils/language";

const defaultLocale = "en";

Expand All @@ -10,7 +11,13 @@ LANGUAGES.forEach(([name, code, flag]) => {
}
});

// handle two-part locales
register("en-US", () => import("@/langs/json/en.json"));
register("en-GB", () => import("@/langs/json/en.json"));

init({
fallbackLocale: defaultLocale,
initialLocale: browser ? window.navigator.language : defaultLocale,
initialLocale: browser
? getLanguage(window.navigator?.language ?? defaultLocale)
: defaultLocale,
});
6 changes: 6 additions & 0 deletions src/lib/utils/language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// handle language parsing here so we can test it

export function getLanguage(code: string, fallback: string = "en") {
const [language, ...tags] = code.split("-");
return language || fallback;
}
19 changes: 19 additions & 0 deletions src/lib/utils/tests/language.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, test, expect } from "vitest";
import { getLanguage } from "../language";

// code, expected
const languages = [
["en", "en"],
["en-US", "en"],
["en-GB", "en"],
["es", "es"],
["--", "en"],
];

describe("language", () => {
test("split language codes", () => {
for (const [code, expected] of languages) {
expect(getLanguage(code as string)).toEqual(expected);
}
});
});
8 changes: 6 additions & 2 deletions src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { browser } from "$app/environment";
import { locale, waitLocale } from "svelte-i18n";
import { getLanguage } from "$lib/utils/language";

import "$lib/i18n/index.js"; // Import to initialize. Important :)

export const trailingSlash = "always";

export async function load() {
if (browser) {
const language =
localStorage.getItem("dc-locale") || window.navigator.language;
const lang = localStorage.getItem("dc-locale") || window.navigator.language;

// use en.json for en-US and such
const language = getLanguage(lang, "en");

locale.set(language);
}
await waitLocale();
Expand Down

0 comments on commit fd77515

Please sign in to comment.