Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize on 'en', not 'en-US' as a language code, but register both #868

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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],
eyeseast marked this conversation as resolved.
Show resolved Hide resolved
};

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"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to handle other two part locales? Spanish is spoken in a lot of countries.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure these are strictly necessary. I think part of the issue was that we were defaulting (in hooks.server.ts) to en-US and that wasn't registered.


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