translation-table.tsx


Overview

The translation-table.tsx file defines a reusable React functional component named TranslationTable that renders a dynamic, sortable, and filterable table of translation keys and their corresponding translations in multiple languages. Leveraging the Table component from the Ant Design (antd) UI library, this component is designed to facilitate the display and exploration of multilingual translation data in a structured tabular format.

Key features include:

This component is useful in localization management tools, content review interfaces, or admin panels where translation data needs to be inspected or edited (though editing is not provided here).


Detailed Explanation

Types and Interfaces

TranslationTableRow

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

Example:

{
  key: "greeting",
  en: "Hello",
  fr: "Bonjour",
  es: "Hola"
}

TranslationTableProps

interface TranslationTableProps {
  data: TranslationTableRow[];
  languages: string[];
}

Component: TranslationTable

const TranslationTable: React.FC<TranslationTableProps> = ({ data, languages }) => { ... }

Purpose

Renders a translation table with dynamic columns, sorting, and filtering capabilities.


Parameters


Implementation Details

  1. Columns Definition

    • Starts with a fixed "Key" column:

      • Fixed to the left.

      • Width of 200 pixels.

      • Sortable alphabetically by the translation key.

    • Dynamically appends one column per language:

      • Title is the language code (e.g., "en").

      • Sortable alphabetically by the translation string in that language.

      • Includes filters:

        • "Show Empty": filters rows where the translation value is empty or missing.

        • "Show Non-Empty": filters rows where the translation value exists and is not empty.

      • Filtering logic based on the selected filter value applies per language column.

  2. Table Configuration

    • Uses Ant Design's Table component.

    • columns and dataSource are passed accordingly.

    • rowKey is set to "key" to uniquely identify rows.

    • Pagination set with a page size of 10 entries.

    • Horizontal scroll enabled (scroll={{ x: true }}) to handle many language columns.


Return Value

Returns a JSX element rendering the configured Table.


Usage Example

import React from 'react';
import TranslationTable from './translation-table';

const data = [
  { key: 'welcome', en: 'Welcome', fr: 'Bienvenue', es: 'Bienvenido' },
  { key: 'bye', en: 'Goodbye', fr: '', es: 'Adiós' },
];

const languages = ['en', 'fr', 'es'];

const App = () => (
  <TranslationTable data={data} languages={languages} />
);

export default App;

Important Implementation Details


Interaction with Other System Components


Diagram: Component Structure and Data Flow

componentDiagram
    component TranslationTable {
        +props: TranslationTableProps
        +render(): JSX.Element
    }
    component Table (Ant Design Table)
    TranslationTable --> Table : renders with dynamic columns and data

    TranslationTableProps <|-- TranslationTable : receives data, languages
    TranslationTableProps : data: TranslationTableRow[]
    TranslationTableProps : languages: string[]

    note right of TranslationTable
      - Defines columns dynamically based on languages
      - Sorting and filtering per column
      - Fixed key column
      - Pagination enabled
    end note

Summary

The translation-table.tsx file provides a flexible, well-typed React component that dynamically creates a sortable and filterable translation table for multiple languages. Built atop Ant Design's Table, it is ideal for translation management interfaces, allowing users to efficiently view and filter multilingual content. The file cleanly separates data structure definitions, UI logic, and configuration, enabling easy integration and extension.