opral / inlang-paraglide-js

Tree-shakable i18n library build on the inlang ecosystem.
https://inlang.com/m/gerre34r/library-inlang-paraglideJs
38 stars 0 forks source link

make `setLanguageTag` work in every version of paraglide #187

Open samuelstroschein opened 1 month ago

samuelstroschein commented 1 month ago

Context

Users are repeatedly asking why setLanguageTag() is not working.

Proposal

Let setLanguageTag() set the language tag programmatically.

Additional information

Related to #185 which should prevent knowledge transfer/variants behaving differently issues like these

elron commented 1 month ago

I needed this too, so I created a custom helper function until theres an official function.

Hope this helps:

// helpers/switchLanguage.ts// helpers/switchLanguage.ts
import { goto } from '$app/navigation';
import { languageTag } from '$lib/paraglide/runtime';
import { get } from 'svelte/store';
import { page } from '$app/stores';
import { derived } from 'svelte/store';

// decodeURI for hebrew in url
export const langless_pathname = derived(page, ($page) =>
  // removes /[language_code] from url and keeps the rest
  decodeURI($page.url.pathname.replace('/' + languageTag(), '') || '/')
);

export function switchLanguage(lang: string, replaceState = false) {
  goto(`/${lang}${get(langless_pathname)}`, { replaceState });
}