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

draft: i18n #55

Closed
wants to merge 5 commits into from
Closed
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
50 changes: 44 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"@popperjs/core": "^2.11.6",
"@sentry/svelte": "^7.36.0",
"@sentry/tracing": "^7.36.0",
"i18next": "^21.9.2",
"i18next-resources-to-backend": "^1.0.0",
"analytics": "^0.8.1",
"echarts": "^5.4.1",
"pretty-bytes": "^6.1.0",
Expand Down
76 changes: 76 additions & 0 deletions src/lib/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import i18next from 'i18next';
import type { i18n } from 'i18next';
import { writable, type Readable, type Writable } from 'svelte/store';
import resourcesToBackend from 'i18next-resources-to-backend';

export const supportedLanguages = ['de', 'en'];

interface TranslationService {
i18n: Readable<i18n>;
}

export const isLoading = writable(true);

export class I18NextTranslationStore implements TranslationService {
public i18n: Readable<i18n>;
public isLoading: Writable<boolean>;

constructor(i18n: i18n) {
this.i18n = this.createInstance(i18n);
this.isLoading = this.createLoadingInstance(i18n);
}

private createInstance(i18n: i18n): Writable<i18n> {
const i18nWritable = writable(i18n);

i18n.on('initialized', () => {
i18nWritable.set(i18n);
});
i18n.on('loaded', () => {
i18nWritable.set(i18n);
});
i18n.on('added', () => i18nWritable.set(i18n));
i18n.on('languageChanged', () => {
i18nWritable.set(i18n);
});
return i18nWritable;
}

private createLoadingInstance(i18n: i18n): Writable<boolean> {
// if loaded resources are empty || {}, set loading to true
i18n.on('loaded', (resources) => {
Object.keys(resources).length !== 0 && isLoading.set(false);
});

// if resources failed loading, set loading to true
i18n.on('failedLoading', () => {
isLoading.set(true);
});

return isLoading;
}
}

export const createI18nStore = () => {
i18next
.use(
resourcesToBackend((language, _namespace, callback) => {
import(`$lib/translations/${language}.json`)
.then((resources) => {
callback(null, resources);
})
.catch((error) => {
callback(error, null);
});
})
)
.init({
lng: 'en',
fallbackLng: 'en'
});
const i18nStore = new I18NextTranslationStore(i18next);

return i18nStore.i18n;
};

export const _ = createI18nStore();
12 changes: 10 additions & 2 deletions src/lib/layout/header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import DarkMode from '$lib/images/mode/dark-mode.svg';
import SystemMode from '$lib/images/mode/system-mode.svg';
import { FeedbackNPS } from '$lib/components';

let showFeedback = false;
import { slide } from 'svelte/transition';
import { page } from '$app/stores';
import { trackEvent } from '$lib/actions/analytics';
import { sdkForConsole } from '$lib/stores/sdk';
import { goto } from '$app/navigation';
import { _ } from '$lib/i18n';

let showFeedback = false;
let showDropdown = false;
let droplistElement: HTMLDivElement;

Expand Down Expand Up @@ -137,6 +137,14 @@
}}>
New organization
</DropListItem>
<DropListItem
on:click={() => {
$_.changeLanguage('en');
}}>🇬🇧</DropListItem>
<DropListItem
on:click={() => {
$_.changeLanguage('de');
}}>🇩🇪</DropListItem>
<DropListLink
href={`${base}/console/account`}
on:click={() => (showDropdown = false)}>
Expand Down
33 changes: 33 additions & 0 deletions src/lib/translations/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"globals": {},
"models": {
"user": {
"id": "ID",
"name": "Name",
"email": "Email",
"status": "Status",
"password": "Passwort",
"joined": "Beigetreten",
"verified": "verifiziert",
"unverified": "unverifiziert",
"blocked": "blockiert"
}
},
"services": {
"authentication": {
"title": "Authentifizierung",
"users": {
"title": "Benutzer",
"search": "Suche nach $t(models.user.name), $t(models.user.email) oder $t(models.user.id)",
"create": "Benutzer erstellen"
},
"create": {
"users": {
"name": "$t(models.user.name) eingeben",
"email": "$t(models.user.email) eingeben",
"password": "$t(models.user.password) eingeben"
}
}
}
}
}
33 changes: 33 additions & 0 deletions src/lib/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"globals": {},
"models": {
"user": {
"id": "ID",
"name": "Name",
"email": "Email",
"status": "Status",
"password": "Password",
"joined": "Joined",
"verified": "verified",
"unverified": "unverified",
"blocked": "blocked"
}
},
"services": {
"authentication": {
"title": "Authentication",
"users": {
"title": "Users",
"search": "Search by $t(models.user.name), $t(models.user.email), or $t(models.user.id)",
"create": "Create user"
},
"create": {
"users": {
"name": "Enter $t(models.user.name)",
"email": "Enter $t(models.user.email)",
"password": "Enter $t(models.user.password)"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import Create from '../createUser.svelte';
import type { Models } from '@aw-labs/appwrite-console';
import type { PageData } from './$types';
import { _ } from '$lib/i18n';

export let data: PageData;

Expand All @@ -38,7 +39,7 @@
</script>

<Container>
<SearchQuery search={data.search} placeholder="Search by name, email, phone, or ID">
<SearchQuery search={data.search} placeholder={$_.t('services.authentication.users.search')}>
<Button on:click={() => (showCreate = true)} event="create_user">
<span class="icon-plus" aria-hidden="true" /> <span class="text">Create user</span>
</Button>
Expand Down