{"version":3,"file":"t.mjs","names":["getI18nConfig","isReadonlyConditionStoreInitialized","getReadonlyConditionStore"],"sources":["../../../src/functions/translation/t.ts"],"sourcesContent":["import {\n  createLookupOptions,\n  getRuntimeEnvironment,\n  interpolateMessage,\n} from 'gt-i18n/internal';\nimport type { GTTranslationOptions } from 'gt-i18n/types';\nimport type { LookupOptionsFor } from 'gt-i18n/internal/types';\nimport { getI18nConfig } from '../../setup/i18nConfig';\nimport {\n  getReadonlyConditionStore,\n  isReadonlyConditionStoreInitialized,\n} from '../../condition-store/singleton-operations';\nimport { StringContent, StringFormat } from 'generaltranslation/types';\nimport { getReactI18nCache } from '../../i18n-cache/singleton-operations';\nimport { getShouldTranslate } from '../../hooks/utils/getShouldTranslate';\nimport { createDiagnosticMessage } from 'generaltranslation/internal';\n\n/**\n * Translate a message\n * @param {string} message - The message to translate.\n * @param {GTTranslationOptions} [options] - The options for the translation.\n * @returns {string} The translated message.\n *\n * @example\n * t('Hello, world!'); // Translates 'Hello, world!'\n *\n * @example\n * t('Hello, {name}!', { name: 'John' }); // Translates 'Hello, John!'\n *\n * @example\n * t`Hello, ${name}` // Translate via tagged template literal\n *\n */\nexport const t: StringOrTemplateSyncResolutionFunction = (\n  messageOrStrings: string | TemplateStringsArray,\n  ...values: unknown[]\n) => {\n  // Warnings and errors\n  enforceSSRRules(messageOrStrings);\n\n  //  t(\"Hello, {name}!\", { name: \"John\" })\n  if (typeof messageOrStrings === 'string') {\n    const options = values.at(0) as GTTranslationOptions | undefined;\n    const locale = options?.$locale ?? getLocale();\n    return resolveStringContent(\n      locale,\n      messageOrStrings,\n      createLookupOptions(locale, options ?? {}, 'ICU')\n    );\n  }\n\n  // t`Hello, ${name}`\n  return handleTaggedTemplateLiteralTranslation(messageOrStrings, values);\n};\n\n// ----- Helper Functions ----- //\n\nexport function resolveStringContent(\n  locale: string,\n  content: StringContent,\n  options: LookupOptionsFor<StringFormat> = {}\n): StringContent {\n  const i18nCache = getReactI18nCache();\n  const defaultLocale = getI18nConfig().getDefaultLocale();\n  if (!getShouldTranslate()) {\n    return interpolateMessage({\n      options,\n      source: content,\n      sourceLocale: defaultLocale,\n    });\n  }\n\n  const lookupOptions = createLookupOptions(locale, options, 'ICU');\n  const translation = i18nCache.lookupTranslation(\n    lookupOptions.$locale,\n    content,\n    lookupOptions\n  );\n  return interpolateMessage({\n    source: content,\n    target: translation,\n    options: lookupOptions,\n    sourceLocale: defaultLocale,\n  });\n}\n\n/**\n * Handle tagged template literal translation\n * @param messageOrStrings - The message or strings to translate.\n * @param values - The values to interpolate.\n * @returns The translated message.\n *\n * This is triggered when there has been no compiler transformation\n *\n * Try looking up interpolated template first\n * If not found, resolve uninterpolated message\n */\nfunction handleTaggedTemplateLiteralTranslation(\n  messageOrStrings: TemplateStringsArray,\n  values: unknown[]\n): string {\n  const locale = getLocale();\n  // for tagged template literals, there has been no compiler transformation\n  // (1) lookup interpolated template (aka derived message)\n  const interpolatedTemplate = interpolateTemplateLiteral(\n    messageOrStrings,\n    values\n  );\n  const i18nCache = getReactI18nCache();\n  const translatedInterpolatedTemplate = i18nCache.lookupTranslation(\n    locale,\n    interpolatedTemplate,\n    { $format: 'STRING' }\n  );\n  if (translatedInterpolatedTemplate) return translatedInterpolatedTemplate;\n\n  // (2) resolve uninterpolated message\n  const { message, variables } = extractInterpolatableValues(\n    messageOrStrings,\n    values\n  );\n  return resolveStringContent(locale, message, variables);\n}\n\n/**\n * Given a TemplateStringsArray, and values, return the uninterpolated message and variables.\n * @param strings - The template strings.\n * @param values - The values to interpolate.\n * @returns The interpolated message and variables.\n */\nfunction extractInterpolatableValues(\n  strings: TemplateStringsArray,\n  values: unknown[]\n): {\n  message: string;\n  variables: Record<string, unknown>;\n} {\n  // String parts\n  const parts: string[] = [];\n  // Variables\n  const variables: Record<string, unknown> = {};\n  let varIndex = 0;\n\n  for (let i = 0; i < strings.length; i++) {\n    // Add the cooked text from the quasi (use cooked to handle escape sequences)\n    parts.push(strings[i]);\n\n    // If there's a corresponding expression, create a variable placeholder\n    if (i < values.length) {\n      const key = varIndex.toString();\n      parts.push(`{${key}}`);\n      variables[key] = values[i];\n      varIndex++;\n    }\n  }\n\n  return {\n    message: parts.join(''),\n    variables,\n  };\n}\n\n/**\n * Interpolate a template literal\n * @param message - The message to interpolate.\n * @param variables - The variables to interpolate.\n * @returns The interpolated message.\n */\nfunction interpolateTemplateLiteral(\n  strings: TemplateStringsArray,\n  values: unknown[]\n): string {\n  return strings\n    .map((string, index) => {\n      return string + (values[index] ?? '');\n    })\n    .join('');\n}\n\n/**\n * Module-level t() only works for SPA.\n * We have to error or fallback in SSR.\n */\nfunction enforceSSRRules(messageOrStrings: string | TemplateStringsArray) {\n  const ssrEnabled = getI18nConfig().getRenderStrategy() === 'server-render';\n  const moduleLevel = !isReadonlyConditionStoreInitialized();\n  if (!ssrEnabled || !moduleLevel) return;\n\n  const message =\n    typeof messageOrStrings === 'string'\n      ? messageOrStrings\n      : messageOrStrings.join('');\n  const runtimeEnvironment = getRuntimeEnvironment();\n  const errorMessage = createDiagnosticMessage({\n    source: '@generaltranslation/react-core',\n    severity: 'Error',\n    whatHappened:\n      'Using the t() function at the module level is forbidden in server-rendered applications.',\n    fix: 'Either move the t() invocation into a request-time scope or register the string with the msg() function and translate with an m() function. Ensure that you have added the <GTProvider> at the root of your component tree.',\n    wayOut:\n      runtimeEnvironment === 'development'\n        ? undefined\n        : 'Falling back to defaultLocale value.',\n    details: `Message: \"${message}\"`,\n  });\n  if (getRuntimeEnvironment() === 'development') {\n    throw new Error(errorMessage);\n  } else {\n    console.error(errorMessage);\n  }\n}\n\nfunction getLocale(): string {\n  return getReadonlyConditionStore().getLocale();\n}\n\n/**\n * Overloaded type for the `t` function.\n * - Tagged template: t`Hello, ${name}` (transformed by the compiler plugin at build time)\n * - Function call: t(\"Hello, {name}\", { name: \"John\" })\n *\n * {@link TemplateSyncResolutionFunction}\n * {@link SyncResolutionFunction}\n */\ninterface StringOrTemplateSyncResolutionFunction {\n  (strings: TemplateStringsArray, ...values: unknown[]): string;\n  (message: string, options?: GTTranslationOptions): string;\n}\n\n/**\n * Type for the `t` function when used as a tagged template literal.\n * @param strings - The template strings.\n * @param values - The values to interpolate.\n * @returns The translated message.\n */\ntype TemplateSyncResolutionFunction = (\n  strings: TemplateStringsArray,\n  ...values: unknown[]\n) => string;\n"],"mappings":"2fAiCA,MAAa,GACX,EACA,GAAG,IACA,CAKH,GAHA,EAAgB,EAAiB,CAG7B,OAAO,GAAqB,SAAU,CACxC,IAAM,EAAU,EAAO,GAAG,EAAE,CACtB,EAAS,GAAS,SAAW,GAAW,CAC9C,OAAO,EACL,EACA,EACA,EAAoB,EAAQ,GAAW,EAAE,CAAE,MAAM,CAClD,CAIH,OAAO,EAAuC,EAAkB,EAAO,EAKzE,SAAgB,EACd,EACA,EACA,EAA0C,EAAE,CAC7B,CACf,IAAM,EAAY,GAAmB,CAC/B,EAAgBA,GAAe,CAAC,kBAAkB,CACxD,GAAI,CAAC,GAAoB,CACvB,OAAO,EAAmB,CACxB,UACA,OAAQ,EACR,aAAc,EACf,CAAC,CAGJ,IAAM,EAAgB,EAAoB,EAAQ,EAAS,MAAM,CAMjE,OAAO,EAAmB,CACxB,OAAQ,EACR,OAPkB,EAAU,kBAC5B,EAAc,QACd,EACA,EAImB,CACnB,QAAS,EACT,aAAc,EACf,CAAC,CAcJ,SAAS,EACP,EACA,EACQ,CACR,IAAM,EAAS,GAAW,CAGpB,EAAuB,EAC3B,EACA,EACD,CAEK,EADY,GAC8B,CAAC,kBAC/C,EACA,EACA,CAAE,QAAS,SAAU,CACtB,CACD,GAAI,EAAgC,OAAO,EAG3C,GAAM,CAAE,UAAS,aAAc,EAC7B,EACA,EACD,CACD,OAAO,EAAqB,EAAQ,EAAS,EAAU,CASzD,SAAS,EACP,EACA,EAIA,CAEA,IAAM,EAAkB,EAAE,CAEpB,EAAqC,EAAE,CACzC,EAAW,EAEf,IAAK,IAAI,EAAI,EAAG,EAAI,EAAQ,OAAQ,IAKlC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