Initial commit of YouTube Downloader application, including core functionality for downloading videos, user authentication, and Docker support. Added configuration files, environment setup, and basic UI components using Astro.js and Tailwind CSS.
This commit is contained in:
101
src/lib/i18n.ts
Normal file
101
src/lib/i18n.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import type { AstroGlobal, Request } from 'astro';
|
||||
import enTranslations from '../i18n/en.json';
|
||||
import deTranslations from '../i18n/de.json';
|
||||
|
||||
type Translations = typeof enTranslations;
|
||||
|
||||
const translations: Record<string, Translations> = {
|
||||
en: enTranslations,
|
||||
de: deTranslations,
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the current locale from Astro
|
||||
*/
|
||||
export function getLocale(astro: AstroGlobal): string {
|
||||
// Check environment variable first (for Docker/container environments)
|
||||
const envLocale = import.meta.env.LOCALE;
|
||||
if (envLocale && (envLocale === 'de' || envLocale === 'en')) {
|
||||
return envLocale;
|
||||
}
|
||||
return astro.locale || 'de';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get locale from Request (for API routes)
|
||||
*/
|
||||
export function getLocaleFromRequest(request: Request): string {
|
||||
// Check environment variable first (for Docker/container environments)
|
||||
const envLocale = import.meta.env.LOCALE;
|
||||
if (envLocale && (envLocale === 'de' || envLocale === 'en')) {
|
||||
return envLocale;
|
||||
}
|
||||
|
||||
// Try to get locale from Accept-Language header
|
||||
const acceptLanguage = request.headers.get('accept-language');
|
||||
if (acceptLanguage) {
|
||||
if (acceptLanguage.includes('de')) return 'de';
|
||||
if (acceptLanguage.includes('en')) return 'en';
|
||||
}
|
||||
return 'de'; // Default to German
|
||||
}
|
||||
|
||||
/**
|
||||
* Get translation for a key
|
||||
* Supports nested keys like "common.download"
|
||||
* Supports interpolation with {{variable}}
|
||||
*/
|
||||
function getTranslation(locale: string, key: string, params?: Record<string, string>): string {
|
||||
const translation = translations[locale] || translations.de;
|
||||
|
||||
// Navigate through nested keys
|
||||
const keys = key.split('.');
|
||||
let value: any = translation;
|
||||
|
||||
for (const k of keys) {
|
||||
if (value && typeof value === 'object' && k in value) {
|
||||
value = value[k];
|
||||
} else {
|
||||
// Fallback to German if key not found
|
||||
value = deTranslations;
|
||||
for (const k2 of keys) {
|
||||
if (value && typeof value === 'object' && k2 in value) {
|
||||
value = value[k2];
|
||||
} else {
|
||||
return key; // Return key if translation not found
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof value !== 'string') {
|
||||
return key;
|
||||
}
|
||||
|
||||
// Replace placeholders like {{variable}}
|
||||
if (params) {
|
||||
return value.replace(/\{\{(\w+)\}\}/g, (match, paramKey) => {
|
||||
return params[paramKey] || match;
|
||||
});
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get translation in Astro component
|
||||
*/
|
||||
export function t(astro: AstroGlobal, key: string, params?: Record<string, string>): string {
|
||||
const locale = getLocale(astro);
|
||||
return getTranslation(locale, key, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get translation in API route
|
||||
*/
|
||||
export function tApi(request: Request, key: string, params?: Record<string, string>): string {
|
||||
const locale = getLocaleFromRequest(request);
|
||||
return getTranslation(locale, key, params);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user