feat: add Polish and English language switcher
This commit is contained in:
@@ -8,8 +8,9 @@ import type {
|
||||
WeatherWarning,
|
||||
WarningKind,
|
||||
} from "@/types/imgw";
|
||||
import { translate, type Language } from "@/lib/i18n";
|
||||
|
||||
const polishLocale = "pl-PL";
|
||||
const locales: Record<Language, string> = { pl: "pl-PL", en: "en-GB" };
|
||||
|
||||
export function toNumber(value: unknown): number | null {
|
||||
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
||||
@@ -70,7 +71,7 @@ export function normalizeWarning(raw: RawWarning, kind: WarningKind, index: numb
|
||||
.map((area) => area.opis?.trim() || area.wojewodztwo?.trim())
|
||||
.filter((area): area is string => Boolean(area));
|
||||
const areas = describedAreas.length ? describedAreas : (raw.teryt ?? []).map((code) => `TERYT ${code}`);
|
||||
const title = raw.zdarzenie?.trim() || raw.nazwa_zdarzenia?.trim() || (kind === "meteo" ? "Ostrzeżenie meteorologiczne" : "Ostrzeżenie hydrologiczne");
|
||||
const title = raw.zdarzenie?.trim() || raw.nazwa_zdarzenia?.trim() || "";
|
||||
return {
|
||||
id: `${kind}-${raw.id ?? raw.numer ?? index}-${raw.data_od ?? raw.obowiazuje_od ?? "unknown"}`,
|
||||
kind,
|
||||
@@ -87,41 +88,41 @@ export function normalizeWarning(raw: RawWarning, kind: WarningKind, index: numb
|
||||
};
|
||||
}
|
||||
|
||||
export function formatTemperature(value: number | null) {
|
||||
return value === null ? "Brak danych" : `${Math.round(value)}°`;
|
||||
export function formatTemperature(value: number | null, language: Language = "pl") {
|
||||
return value === null ? translate(language, "common.noData") : `${Math.round(value)}°`;
|
||||
}
|
||||
|
||||
export function formatPressure(value: number | null) {
|
||||
return value === null ? "Brak danych" : `${value.toFixed(1)} hPa`;
|
||||
export function formatPressure(value: number | null, language: Language = "pl") {
|
||||
return value === null ? translate(language, "common.noData") : `${value.toFixed(1)} hPa`;
|
||||
}
|
||||
|
||||
export function formatHumidity(value: number | null) {
|
||||
return value === null ? "Brak danych" : `${Math.round(value)}%`;
|
||||
export function formatHumidity(value: number | null, language: Language = "pl") {
|
||||
return value === null ? translate(language, "common.noData") : `${Math.round(value)}%`;
|
||||
}
|
||||
|
||||
export function formatWind(speed: number | null, direction?: number | null) {
|
||||
if (speed === null) return "Brak danych";
|
||||
export function formatWind(speed: number | null, direction?: number | null, language: Language = "pl") {
|
||||
if (speed === null) return translate(language, "common.noData");
|
||||
const directionLabel = direction === null || direction === undefined ? "" : ` ${getWindDirection(direction)}`;
|
||||
return `${speed.toFixed(1)} m/s${directionLabel}`;
|
||||
}
|
||||
|
||||
export function formatRainfall(value: number | null) {
|
||||
return value === null ? "Brak danych" : `${value.toFixed(value < 1 ? 2 : 1)} mm`;
|
||||
export function formatRainfall(value: number | null, language: Language = "pl") {
|
||||
return value === null ? translate(language, "common.noData") : `${value.toFixed(value < 1 ? 2 : 1)} mm`;
|
||||
}
|
||||
|
||||
export function formatWaterLevel(value: number | null) {
|
||||
return value === null ? "Brak danych" : `${Math.round(value)} cm`;
|
||||
export function formatWaterLevel(value: number | null, language: Language = "pl") {
|
||||
return value === null ? translate(language, "common.noData") : `${Math.round(value)} cm`;
|
||||
}
|
||||
|
||||
export function formatFlow(value: number | null) {
|
||||
return value === null ? "Brak danych" : `${value.toFixed(2)} m³/s`;
|
||||
export function formatFlow(value: number | null, language: Language = "pl") {
|
||||
return value === null ? translate(language, "common.noData") : `${value.toFixed(2)} m³/s`;
|
||||
}
|
||||
|
||||
export function formatDateTime(value: string | null, fallback = "Brak danych") {
|
||||
export function formatDateTime(value: string | null, language: Language = "pl", fallback = translate(language, "common.noData")) {
|
||||
if (!value) return fallback;
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) return fallback;
|
||||
return new Intl.DateTimeFormat(polishLocale, {
|
||||
return new Intl.DateTimeFormat(locales[language], {
|
||||
day: "numeric",
|
||||
month: "long",
|
||||
hour: "2-digit",
|
||||
@@ -167,12 +168,12 @@ export function getWeatherMoodFromData(station: SynopStation, date = new Date())
|
||||
return "mild";
|
||||
}
|
||||
|
||||
export function getWeatherDescription(station: SynopStation) {
|
||||
if ((station.rainfall ?? 0) >= 5) return "Wyraźne opady";
|
||||
if ((station.rainfall ?? 0) >= 0.1) return "Opady";
|
||||
if ((station.windSpeed ?? 0) >= 8) return "Silny wiatr";
|
||||
if ((station.humidity ?? 0) >= 90) return "Wilgotno";
|
||||
return "Spokojne warunki";
|
||||
export function getWeatherDescription(station: SynopStation, language: Language = "pl") {
|
||||
if ((station.rainfall ?? 0) >= 5) return translate(language, "weather.heavyRain");
|
||||
if ((station.rainfall ?? 0) >= 0.1) return translate(language, "weather.rain");
|
||||
if ((station.windSpeed ?? 0) >= 8) return translate(language, "weather.strongWind");
|
||||
if ((station.humidity ?? 0) >= 90) return translate(language, "weather.humid");
|
||||
return translate(language, "weather.calm");
|
||||
}
|
||||
|
||||
export function moodGradient(mood: WeatherMood) {
|
||||
|
||||
Reference in New Issue
Block a user