All files / src/flow flow-translations.ts

90.07% Statements 227/252
65.78% Branches 50/76
100% Functions 3/3
90.07% Lines 227/252

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 25396x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 1794x     1794x 1794x 486x 486x 486x 486x 486x 1308x 1794x                 1308x 1794x     1308x 1308x 1308x 1308x 1308x       96x 96x 813x 813x 813x 813x 813x 813x 813x 813x 813x 830x     830x 830x 830x 830x 830x 830x 830x 830x 830x 830x 830x 830x     830x 830x 362x 362x 830x 830x 830x 830x     830x 830x 830x     830x 830x 830x 830x 1190x 486x 604x 486x 486x 1190x     704x 1190x 830x 830x 830x 830x 830x 830x 830x 830x 830x 830x 830x 830x 830x 813x 813x 813x 813x 96x 96x 96x 96x 96x 96x 96x 209x 209x 96x 96x 483x 96x 96x 96x 96x 766x 766x 766x 766x 766x 766x 766x 766x 766x 95x 766x 766x 749x 749x 766x 112x 766x 766x 766x 766x 766x 766x 766x 766x 766x 766x 766x 766x 766x 766x 766x 766x 766x 96x 96x 96x 96x 766x 181x 181x 96x 173x 165x 165x 95x 95x 165x 96x 165x 221x 221x 221x 221x 221x 221x 221x 221x 165x 221x 221x 221x 165x 165x 165x 165x 165x 181x 181x 181x 181x 181x 173x 173x 173x 96x 103x 96x 103x 103x 95x 103x 103x 103x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 111x 111x 111x 111x 111x 96x 96x 96x  
import { FlowDefinition } from '../store/flow-definition';
import { ACTION_CONFIG, NODE_CONFIG } from './config';
import { getTranslatableCategoriesForNode } from './categoryLocalization';
 
export type TranslationType = 'property' | 'category';
 
export interface TranslationEntry {
  uuid: string;
  type: TranslationType;
  attribute: string;
  from: string;
  to: string | null;
  // Original array form of the source/target. For attributes whose value
  // is an array (e.g. router case `arguments`), these preserve the per-item
  // structure that `from`/`to` would otherwise lose to comma-joining. For
  // scalar attributes the arrays contain a single item.
  fromValues: string[];
  toValues: string[] | null;
}
 
export interface TranslationBundle {
  nodeUuid: string;
  actionUuid?: string;
  translations: TranslationEntry[];
}
 
export function formatTranslationValue(value: any): string | null {
  if (value === null || value === undefined) {
    return null;
  }
 
  if (Array.isArray(value)) {
    const normalized = value
      .map((entry) => formatTranslationValue(entry))
      .filter((entry) => !!entry) as string[];
    return normalized.length > 0 ? normalized.join(', ') : null;
  }
 
  if (typeof value === 'object') {
    if ('name' in value && value.name) {
      return String(value.name);
    }
    if ('arguments' in value && Array.isArray(value.arguments)) {
      return value.arguments.join(' ');
    }
    return null;
  }
 
  if (typeof value === 'number') {
    return value.toString();
  }
 
  if (typeof value === 'string') {
    const trimmed = value.trim();
    return trimmed.length > 0 ? trimmed : null;
  }

  return null;
}
 
export function findTranslations(
  type: TranslationType,
  uuid: string,
  localizableKeys: string[],
  source: any,
  localization: Record<string, any>
): TranslationEntry[] {
  const translations: TranslationEntry[] = [];
 
  localizableKeys.forEach((attribute) => {
    if (attribute === 'quick_replies') {
      return;
    }
 
    const pathSegments = attribute.split('.');
    let from: any = source;
    let to: any = [];
 
    while (pathSegments.length > 0 && from) {
      if (from.uuid) {
        to = localization[from.uuid];
      }
 
      const path = pathSegments.shift();
      if (!path) {
        break;
      }
 
      if (to) {
        to = to[path];
      }
      from = from[path];
    }
 
    if (!from) {
      return;
    }
 
    const fromValue = formatTranslationValue(from);
    if (!fromValue) {
      return;
    }
 
    const toValue = to ? formatTranslationValue(to) : null;
 
    const toArray = (value: any): string[] => {
      if (Array.isArray(value)) {
        return value.map((v) =>
          v === null || v === undefined ? '' : String(v)
        );
      }
      if (value === null || value === undefined) {
        return [];
      }
      return [String(value)];
    };
 
    const fromValues = toArray(from);
    const toValues = to !== null && to !== undefined ? toArray(to) : null;
 
    translations.push({
      uuid,
      type,
      attribute,
      from: fromValue,
      to: toValue,
      fromValues,
      toValues
    });
  });
 
  return translations;
}
 
export function buildTranslationBundles(
  definition: FlowDefinition | null | undefined,
  languageCode: string
): TranslationBundle[] {
  if (!definition || !languageCode || languageCode === definition.language) {
    return [];
  }
 
  const languageLocalization = definition.localization?.[languageCode] || {};
  const bundles: TranslationBundle[] = [];
 
  definition.nodes.forEach((node) => {
    node.actions?.forEach((action) => {
      const config = ACTION_CONFIG[action.type];
      if (!config?.localizable || config.localizable.length === 0) {
        return;
      }
 
      // For send_msg actions, only count 'text' for progress tracking
      // (quick_replies and attachments are still localizable but don't count toward progress)
      const localizableKeys =
        action.type === 'send_msg'
          ? config.localizable.filter((key) => key === 'text')
          : config.localizable;
 
      const translations = findTranslations(
        'property',
        action.uuid,
        localizableKeys,
        action,
        languageLocalization
      );
 
      if (translations.length > 0) {
        bundles.push({
          nodeUuid: node.uuid,
          actionUuid: action.uuid,
          translations
        });
      }
    });
 
    const nodeUI = definition._ui?.nodes?.[node.uuid];
    const nodeType = nodeUI?.type;
    if (!nodeType) {
      return;
    }
 
    if (nodeUI?.config?.localizeRules && node.router?.cases?.length) {
      const ruleTranslations = node.router.cases
        .filter((c) => c.arguments?.length > 0 && c.arguments.some((a) => a))
        .flatMap((c) =>
          findTranslations(
            'property',
            c.uuid,
            ['arguments'],
            c,
            languageLocalization
          )
        );
 
      if (ruleTranslations.length > 0) {
        bundles.push({
          nodeUuid: node.uuid,
          translations: ruleTranslations
        });
      }
    }
 
    const nodeConfig = NODE_CONFIG[nodeType];
    if (
      nodeUI?.config?.localizeCategories &&
      nodeConfig?.localizable === 'categories' &&
      node.router?.categories?.length
    ) {
      const translatableCategories = getTranslatableCategoriesForNode(
        nodeType,
        node.router.categories
      );
      const categoryTranslations = translatableCategories.flatMap((category) =>
        findTranslations(
          'category',
          category.uuid,
          ['name'],
          category,
          languageLocalization
        )
      );
 
      if (categoryTranslations.length > 0) {
        bundles.push({
          nodeUuid: node.uuid,
          translations: categoryTranslations
        });
      }
    }
  });
 
  return bundles;
}
 
export function getTranslationCounts(bundles: TranslationBundle[]): {
  total: number;
  localized: number;
} {
  return bundles.reduce(
    (counts, bundle) => {
      bundle.translations.forEach((translation) => {
        counts.total += 1;
        if (translation.to && translation.to.trim().length > 0) {
          counts.localized += 1;
        }
      });
      return counts;
    },
    { total: 0, localized: 0 }
  );
}