mirror of
https://github.com/umami-software/umami.git
synced 2026-02-19 12:05:41 +01:00
Simplify i18n: remove old react-intl artifacts, rename formatMessage to t, replace FormattedMessage with t.rich().
- Rewrite messages.ts to plain string key maps (remove MessageDescriptor) - Rewrite useMessages hook to expose t from useTranslations() directly - Rename formatMessage → t across 193 consumer files - Replace custom FormattedMessage component with next-intl t.rich() - Update 52 language files to use rich text tags (<b>, <a>) - Remove all direct imports from @/components/messages in favor of useMessages() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
80cad6ea65
commit
50edb71687
247 changed files with 1660 additions and 2194 deletions
|
|
@ -1,47 +1,11 @@
|
|||
import { useTranslations } from 'next-intl';
|
||||
import { createElement, type ReactNode } from 'react';
|
||||
import { labels, type MessageDescriptor, messages } from '@/components/messages';
|
||||
import { labels, messages } from '@/components/messages';
|
||||
import type { ApiError } from '@/lib/types';
|
||||
|
||||
type FormatMessage = (
|
||||
descriptor: MessageDescriptor,
|
||||
values?: Record<string, string | number | boolean | null | undefined>,
|
||||
opts?: any,
|
||||
) => string | null;
|
||||
|
||||
interface FormattedMessageProps extends MessageDescriptor {
|
||||
values?: Record<string, ReactNode>;
|
||||
}
|
||||
|
||||
interface UseMessages {
|
||||
formatMessage: FormatMessage;
|
||||
messages: typeof messages;
|
||||
labels: typeof labels;
|
||||
getMessage: (id: string) => string;
|
||||
getErrorMessage: (error: ApiError) => string | undefined;
|
||||
FormattedMessage: (props: FormattedMessageProps) => ReactNode;
|
||||
}
|
||||
|
||||
export function useMessages(): UseMessages {
|
||||
export function useMessages() {
|
||||
const t = useTranslations();
|
||||
|
||||
const formatMessage = (
|
||||
descriptor: MessageDescriptor,
|
||||
values?: Record<string, string | number | boolean | null | undefined>,
|
||||
) => {
|
||||
if (!descriptor) return null;
|
||||
try {
|
||||
return t(descriptor.id, values);
|
||||
} catch {
|
||||
return descriptor.defaultMessage || descriptor.id;
|
||||
}
|
||||
};
|
||||
|
||||
const getMessage = (id: string) => {
|
||||
const message = Object.values(messages).find(value => value.id === `message.${id}`);
|
||||
|
||||
return message ? formatMessage(message) : id;
|
||||
};
|
||||
const getMessage = (id: string) => t(`message.${id}`);
|
||||
|
||||
const getErrorMessage = (error: ApiError) => {
|
||||
if (!error) {
|
||||
|
|
@ -53,50 +17,5 @@ export function useMessages(): UseMessages {
|
|||
return code ? getMessage(code) : error?.message || 'Unknown error';
|
||||
};
|
||||
|
||||
function FormattedMessage({ id, defaultMessage, values }: FormattedMessageProps) {
|
||||
if (
|
||||
!values ||
|
||||
Object.values(values).every(v => typeof v === 'string' || typeof v === 'number')
|
||||
) {
|
||||
try {
|
||||
return t(id, values as any);
|
||||
} catch {
|
||||
return defaultMessage || id;
|
||||
}
|
||||
}
|
||||
|
||||
// For JSX values: get the raw ICU template and manually interpolate
|
||||
let template: string;
|
||||
try {
|
||||
template = t.raw(id) as string;
|
||||
} catch {
|
||||
template = defaultMessage || id;
|
||||
}
|
||||
|
||||
if (typeof template !== 'string') {
|
||||
return defaultMessage || id;
|
||||
}
|
||||
|
||||
// Split on {placeholder} tokens and interleave with values
|
||||
const parts: ReactNode[] = [];
|
||||
const regex = /\{(\w+)\}/g;
|
||||
let lastIndex = 0;
|
||||
|
||||
for (let match = regex.exec(template); match !== null; match = regex.exec(template)) {
|
||||
if (match.index > lastIndex) {
|
||||
parts.push(template.slice(lastIndex, match.index));
|
||||
}
|
||||
const key = match[1];
|
||||
parts.push(values[key] !== undefined ? values[key] : match[0]);
|
||||
lastIndex = regex.lastIndex;
|
||||
}
|
||||
|
||||
if (lastIndex < template.length) {
|
||||
parts.push(template.slice(lastIndex));
|
||||
}
|
||||
|
||||
return createElement('span', null, ...parts);
|
||||
}
|
||||
|
||||
return { formatMessage, messages, labels, getMessage, getErrorMessage, FormattedMessage };
|
||||
return { t, messages, labels, getMessage, getErrorMessage };
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue