feat(i18n): integrate internationalization for game pages
- Added translation support for various game-related texts in the home, daily, infinite, login, and profile pages. - Replaced hardcoded French strings with translation keys using the `$t` function. - Updated titles, descriptions, and button texts to enhance localization.
This commit is contained in:
51
src/lib/i18n/index.ts
Normal file
51
src/lib/i18n/index.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { writable, derived } from 'svelte/store';
|
||||
import type { Writable, Readable } from 'svelte/store';
|
||||
|
||||
import en from './en.json';
|
||||
import fr from './fr.json';
|
||||
|
||||
type Messages = typeof en;
|
||||
|
||||
const translations: Record<string, Messages> = { en, fr };
|
||||
|
||||
// Get initial language
|
||||
function getInitialLanguage(): string {
|
||||
if (typeof window !== 'undefined') {
|
||||
const stored = localStorage.getItem('language');
|
||||
if (stored && stored in translations) {
|
||||
return stored;
|
||||
}
|
||||
const browserLang = navigator.language.split('-')[0];
|
||||
if (browserLang in translations) {
|
||||
return browserLang;
|
||||
}
|
||||
}
|
||||
return 'en';
|
||||
}
|
||||
|
||||
// Create writable store for the current language
|
||||
export const language: Writable<string> = writable(getInitialLanguage());
|
||||
|
||||
// Create derived store for the current messages
|
||||
export const t: Readable<Messages> = derived(language, ($language) => {
|
||||
return translations[$language] || translations['en'];
|
||||
});
|
||||
|
||||
export function setLanguage(lang: string) {
|
||||
if (lang in translations) {
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('language', lang);
|
||||
}
|
||||
language.set(lang);
|
||||
}
|
||||
}
|
||||
|
||||
export function getLanguage(): string {
|
||||
let currentLang = 'en';
|
||||
language.subscribe((lang) => {
|
||||
currentLang = lang;
|
||||
})();
|
||||
return currentLang;
|
||||
}
|
||||
|
||||
export const availableLanguages = Object.keys(translations);
|
||||
Reference in New Issue
Block a user