diff --git a/app/src/lib/switchToLanguage.test.ts b/app/src/lib/switchToLanguage.test.ts new file mode 100644 index 0000000..32c9797 --- /dev/null +++ b/app/src/lib/switchToLanguage.test.ts @@ -0,0 +1,40 @@ +import { describe, it, expect, vi } from 'vitest'; +import { switchToLanguage } from './switchToLanguage'; +import { i18n } from '$lib/i18n'; +import { goto } from '$app/navigation'; + +vi.mock('$lib/i18n', () => ({ + i18n: { + route: vi.fn().mockImplementation((translatedPath: string) => ''), + resolveRoute: vi.fn().mockImplementation((path: string, lang?: string) => '') + } +})); + +vi.mock('$app/state', () => ({ + page: { + url: { + pathname: '/current-path' + } + } +})); + +vi.mock('$app/navigation', () => ({ + goto: vi.fn() +})); + +describe('switchToLanguage', () => { + it('should switch to the new language', () => { + const newLanguage = 'en'; + const canonicalPath = '/canonical-path'; + const localisedPath = '/en/canonical-path'; + + i18n.route.mockReturnValue(canonicalPath); + i18n.resolveRoute.mockReturnValue(localisedPath); + + switchToLanguage(newLanguage); + + expect(i18n.route).toHaveBeenCalledWith('/current-path'); + expect(i18n.resolveRoute).toHaveBeenCalledWith(canonicalPath, newLanguage); + expect(goto).toHaveBeenCalledWith(localisedPath); + }); +}); \ No newline at end of file diff --git a/app/src/lib/switchToLanguage.ts b/app/src/lib/switchToLanguage.ts new file mode 100644 index 0000000..13d5fc8 --- /dev/null +++ b/app/src/lib/switchToLanguage.ts @@ -0,0 +1,10 @@ +import type { AvailableLanguageTag } from '$lib/paraglide/runtime'; +import { i18n } from '$lib/i18n'; +import { page } from '$app/state'; +import { goto } from '$app/navigation'; + +export function switchToLanguage(newLanguage: AvailableLanguageTag) { + const canonicalPath = i18n.route(page.url.pathname); + const localisedPath = i18n.resolveRoute(canonicalPath, newLanguage); + goto(localisedPath); +} \ No newline at end of file diff --git a/app/src/routes/demo/+page.svelte b/app/src/routes/demo/+page.svelte deleted file mode 100644 index a815390..0000000 --- a/app/src/routes/demo/+page.svelte +++ /dev/null @@ -1 +0,0 @@ -paraglide diff --git a/app/src/routes/demo/paraglide/+page.svelte b/app/src/routes/demo/paraglide/+page.svelte deleted file mode 100644 index c47b06f..0000000 --- a/app/src/routes/demo/paraglide/+page.svelte +++ /dev/null @@ -1,19 +0,0 @@ - - -