wuchale i18n Toolkit

repository·main·Indexed 19 days ago

https://github.com/wuchalejs/wuchale

A compile-time internationalization toolkit that automatically extracts and replaces translatable strings using AST analysis, requiring zero code changes or special i18n syntax. It supports a variety of frameworks via adapters including React, SolidJS (@wuchale/jsx), Svelte (@wuchale/svelte), and Astro (@wuchale/astro), and provides a JSON storage driver (@wuchale/json) for managing translation catalogs.

Tokens
18.9K
Snippets
77
Records
98
Agent score
63%

What's inside wuchale

  1. Overview of wuchale

    main

    wuchale is a compile-time internationalization (i18n) toolkit designed to add translation capabilities to your project without requiring code changes or special syntax. It uses AST analysis to automatically extract translatable text from your source code at build time.

    Key features:

    • No extra syntax: Write standard components; no special t() functions or <Trans> components are required in your source.
    • Framework agnostic: Supports React, Preact, Svelte(Kit), SolidJS, Astro, and plain JS/TS via specific adapters.
    • Smart extraction: Handles nested markup, conditionals, loops, and complex interpolations automatically.
    • Compact catalogs: Translations are compiled into tiny, index-based arrays for minimal bundle size.
    • LLM support: Configurable integration for automatic translations using LLMs.
    • Full URL support: Handles path translations (e.g., /about to /de/uber-uns).
  2. Overview of wuchale i18n toolkit

    main

    What is wuchale?

    wuchale is a compile-time internationalization (i18n) toolkit designed to work with zero code changes. Instead of manually wrapping strings in translation functions, you write your components naturally. During the build process, wuchale automatically extracts translatable strings and replaces them with the appropriate localized versions.

    Key Features

    • Zero code changes: Write standard component code without i18n-specific wrappers.
    • Compile-time extraction: Strings are processed and replaced at build time.
    • Automatic replacement: The toolkit handles the mapping of natural language strings to translation keys automatically.
  3. Use @wuchale/json as a storage driver for wuchale

    main

    @wuchale/json is a JSON catalog storage driver designed for use with the wuchale compile-time internationalization (i18n) toolkit. It allows wuchale to manage and retrieve translatable strings from JSON-based catalogs during the build process.

    npm install @wuchale/json
  4. How wuchale works

    main

    wuchale operates through a four-step build-time process that keeps your source code clean while producing internationalized output:

    1. Scan: Uses AST (Abstract Syntax Tree) analysis to identify translatable text content in your source code.
    2. Extract: Pulls strings into standard .po translation files (or custom storage) for translators.
    3. Compile: Converts translation files into compact modules that export arrays of messages.
    4. Replace: Transforms your original source code into a version that calls translation functions using indices to access messages from the compiled arrays.
  5. Install the @wuchale/jsx adapter

    main

    Use @wuchale/jsx to integrate wuchale into JSX-based projects. This adapter allows you to write components using standard JSX while enabling wuchale to automatically extract and replace translatable strings at build time without requiring manual code changes to your component logic.

    npm install @wuchale/jsx
  6. Integrate wuchale in Astro projects using @wuchale/astro

    main

    The @wuchale/astro package is an adapter designed to integrate the wuchale compile-time internationalization (i18n) toolkit into Astro projects.

    wuchale works by automatically extracting and replacing translatable strings at build time, allowing you to write components naturally without manual i18n code changes. For detailed setup instructions and adapter-specific configuration, refer to the official documentation at wuchale.dev.

  7. Integrate wuchale in Svelte projects using @wuchale/svelte

    main

    The @wuchale/svelte package is an adapter designed to integrate wuchale into Svelte projects. wuchale is a compile-time internationalization (i18n) toolkit that allows you to write components naturally without manual i18n logic in your code. During the build process, wuchale automatically extracts translatable strings and replaces them with the appropriate localized versions.

    For detailed setup and configuration, refer to the official documentation at wuchale.dev.

  8. How AIQueue handles translation batches and retries

    main

    The AIQueue uses a sophisticated mechanism to ensure translation quality and reliability:

    1. Grouping: Items are grouped by locale using the group configuration. This ensures that related languages are sent in the same context to the LLM.
    2. Batching: Items within a group are split into chunks defined by batchSize.
    3. Validation: After receiving a response, AIQueue validates that:
      • The response is a valid JSON array.
      • The number of translated items matches the input.
      • For non-plural items, the translation is semantically equivalent to the source (using isEquivalent).
      • For plural items, the number of translated forms matches the source.
    4. Retries: If validation fails or an error occurs, the queue identifies the unTranslated items and retries the translation for that specific subset. It will attempt this up to 30 times (MAX_RETRIES) before giving up.
  9. Understand Astro runtime behavior in Wuchale

    main

    Because Astro is an SSR (Server-Side Rendering) framework, the @wuchale/astro adapter defaults to a non-reactive runtime.

    • useReactive always returns false.
    • initReactive only returns null (indicating initialization is allowed) if the function being processed is at the top-level or within a top-level function. Otherwise, it returns false to prevent reactivity in nested scopes.
    • reactive.wrapInit and reactive.wrapUse are identity functions (expr => expr), meaning no reactive wrapping is applied.
  10. How AdapterHandler manages the translation lifecycle

    main

    The AdapterHandler follows a specific lifecycle to ensure translations are correctly extracted, stored, and served:

    1. Initialization (create): Sets up file access, loads existing translation data into sharedState, and performs an initial compilation.
    2. Extraction (transform): During file processing, the adapter identifies text items. These items are passed to handleTexts.
    3. Storage Management (handleTexts): New keys are added to the sharedState.catalog, and references (which files use which keys) are tracked. If AI is enabled, items are queued for translation.
    4. Compilation (compile / writeCompiled): The handler iterates through locales and generates optimized JavaScript modules (catalogs) containing the actual translation strings. It supports both global catalogs and granular catalogs (where translations are split by file/ID).
    5. Runtime Injection: The handler prepares the necessary import headers (e.g., getRuntimeVars) so that the transformed code can access the translations via the adapter's runtime API.
  11. Understand Wuchale DevMode options

    main

    The dev property in the configuration determines the development mode behavior. The available modes are:

    • false: Standard production-like mode.
    • 'read': Read-only mode.
    • 'add': Mode for adding new content/locales.
    • 'refs': Mode for managing references.
    • 'clean': Mode for cleaning up files.
  12. Understand the wuchale transformation process

    main

    wuchale transforms natural code into an optimized, index-based runtime.

    Original Source (JSX):

    function Welcome({ user }) {
      return (
        <div>
          <p><b>Hello, {user}</b>, and welcome!</p>
          <button>Get started</button>
        </div>
      )
    }

    Extracted Translation (.po):

    msgid "<0>Hello, {0}</0>, and welcome!"
    msgstr "<0>Hola, {0}</0>, y ¡bienvenido!"
    
    msgid "Get started"
    msgstr "Comenzar"

    Compiled Catalog (JS):

    export let c = [[[0, "Hola, ", 0], ", y ¡bienvenido!"], "Comenzar"]

    Transformed Output (JSX):

    import W_tx_ from "@wuchale/jsx/runtime.jsx"
    
    function Welcome({ user }) {
      const _w_runtime_ = _w_load_rx_();
      return (
        <div>
          <p><W_tx_ t={[_w_ctx_ => <b key="_0"><W_tx_ x={_w_ctx_} n a={[user]} /></b>]} x={_w_runtime_.c(0)} /></p>
          <button>{_w_runtime_(1)}</button>
        </div>
      )
    }

    This process allows for indefinite nesting of markup (like the <b> tag above) while giving translators the freedom to reorder placeholders within the translated string.

    // src/components/Welcome.jsx
    function Welcome({ user }) {
      return (
        <div>
          <p><b>Hello, {user}</b>, and welcome!</p>
          <button>Get started</button>
        </div>
      )
    }