Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

750 B
Raw Blame
export type Messages = { [key: string]: string | Messages };

function lookup(messages: Messages, parts: string[]): string | undefined {
  const value = parts.reduce<string | Messages | undefined>((node, part) => {
    if (node === undefined || typeof node === 'string') return undefined;
    return node[part];
  }, messages);
  return typeof value === 'string' ? value : undefined;
}

export function translate(messages: Messages, key: string, substitutions?: string[]): string {
  const value = lookup(messages, key.split('.')) ?? lookup(messages, key.split('_'));
  if (value === undefined) return key;
  if (!substitutions?.length) return value;
  return value.replace(/\$(\d+)/g, (match, index) => substitutions[Number(index) - 1] ?? match);
}