export function _substituteLocalizations()

in lib/experimentUtils.ts [115:154]


export function _substituteLocalizations(
  values: any,
  localizations?: any,
): object {
  // If the recipe is not localized, we don't need to do anything.
  // Likewise, if the value we are attempting to localize is not an
  // object, there is nothing to localize.
  if (
    typeof localizations === "undefined" ||
    typeof values !== "object" ||
    values === null
  ) {
    return values;
  }

  if (Array.isArray(values)) {
    return values.map((value) =>
      _substituteLocalizations(value, localizations),
    );
  }

  const substituted = Object.assign({}, values);

  // Loop over "$l10n" objects in the recipe and assign the appropriate string IDs from the
  // localizations object
  for (const [key, value] of Object.entries(values)) {
    if (
      key === "$l10n" &&
      typeof value === "object" &&
      value !== null &&
      (value as any)?.id
    ) {
      return localizations[(value as any).id];
    }

    substituted[key] = _substituteLocalizations(value, localizations);
  }

  return substituted;
}