until.ts


Overview

The until.ts file provides utility functions for handling nested localization or translation objects. Its primary purpose is to:

These utilities are particularly useful in internationalization (i18n) workflows where translation strings are often stored in nested JSON objects per language, and a flattened or tabular format is required for easier processing, editing, or exporting (e.g., to CSV or Excel).


Detailed Documentation

Type Definitions

NestedObject

type NestedObject = {
  [key: string]: string | NestedObject;
};

FlattenedObject

type FlattenedObject = {
  [key: string]: string;
};

TranslationTableRow

type TranslationTableRow = {
  key: string;
  [language: string]: string;
};

Functions

flattenObject

export function flattenObject(
  obj: NestedObject,
  parentKey: string = '',
): FlattenedObject
Description

Recursively flattens a nested object into a single-depth object with dot-separated keys.

Parameters
Returns
Implementation Details
Usage Example
const nested = {
  greeting: {
    morning: "Good morning",
    evening: "Good evening",
  },
  farewell: "Goodbye",
};

const flat = flattenObject(nested);
/*
flat = {
  "greeting.morning": "Good morning",
  "greeting.evening": "Good evening",
  "farewell": "Goodbye"
}
*/

createTranslationTable

export function createTranslationTable(
  langs: FlattenedObject[],
  langKeys: string[],
): TranslationTableRow[]
Description

Generates a translation table array from multiple flattened language objects. Each row corresponds to a translation key, with columns for each language's translated string.

Parameters
Returns
Implementation Details
Usage Example
const english = {
  "greeting.morning": "Good morning",
  "farewell": "Goodbye"
};

const vietnamese = {
  "greeting.morning": "Chào buổi sáng",
  "farewell": "Tạm biệt"
};

const table = createTranslationTable([english, vietnamese], ['English', 'Vietnamese']);
/*
table = [
  {
    key: "greeting.morning",
    English: "Good morning",
    Vietnamese: "Chào buổi sáng"
  },
  {
    key: "farewell",
    English: "Goodbye",
    Vietnamese: "Tạm biệt"
  }
]
*/

Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

flowchart TD
    A[NestedObject Input] -->|flattenObject()| B[FlattenedObject Output]
    B --> C[createTranslationTable()]
    C --> D[TranslationTableRow[] Output]

    subgraph Flattening Process
        A --> B
    end

    subgraph Table Creation Process
        B --> C
        C --> D
    end

Summary

until.ts is a focused utility module for:

Its functions are essential for managing complex nested translation data in multi-language applications, facilitating easier editing, comparison, and integration with translation tools.