VoerkaI18n Internationalization Solution

repository·master·Indexed 42 days ago

https://github.com/zhangfisher/voerka-i18n

A comprehensive i18n solution for JavaScript environments including Vue, React, Solid, React Native, and Node.js. VoerkaI18n eliminates manual key mapping by allowing natural language directly in source code, featuring an automated workflow for text extraction, AI-powered translation, and compilation. It is designed for monorepo support with synchronized language state across multiple packages and includes a decoupled architecture separating the toolchain from the runtime.

Tokens
77K
Snippets
256
Records
346
Agent score
49%

What's inside VoerkaI18n

  1. Overview of VoerkaI18n

    master

    VoerkaI18n is a comprehensive internationalization (i18n) solution designed for the JavaScript/TypeScript ecosystem. It supports a wide range of frameworks including Vue, React, Solidjs, Sveltejs, and ReactNative, as well as Node.js environments.

    Key capabilities include:

    • Full Lifecycle Support: Covers text extraction, automatic translation, compilation, and dynamic language switching.
    • Intuitive Workflow: Allows using intuitive translation forms directly in source code instead of managing manual keys.
    • Automated Tooling: Provides scanning and extraction tools to automatically identify translatable text in source files.
    • TypeScript Integration: Built-in support for generating TypeScript types for i18n assets.
    • Dynamic Capabilities: Supports adding new languages dynamically after the application has launched and fixing language patches online.
    • Advanced Interpolation: A powerful mechanism for handling complex variables like dates, currencies, and numbers.
  2. Overview of VoerkaI18n internationalization solution

    master

    VoerkaI18n is an internationalization (i18n) solution designed for JavaScript environments, including Vue, React, Solid, React Native, Node.js, and browsers. Unlike traditional i18n frameworks that require manual key mapping (e.g., $t("message.login")), VoerkaI18n allows you to use natural language (such as Chinese) directly in your source code, automatically handling the translation lifecycle.

    Key Features

    • No Manual Key Mapping: Use text directly in source files without defining artificial keys.
    • Decoupled Architecture: The toolChain is separate from the runtime, ensuring only a minimal footprint is required in production releases.
    • Comprehensive Toolchain: Supports initialization, text extraction, automatic translation, and compilation.
    • Monorepo Friendly: Designed to support international collaboration across multi-library and multi-package monorepo scenarios, allowing sub-packages to switch languages in sync with the main program.
    • Extensible Mechanisms: Includes powerful interpolation via a formatter mechanism and support for plural, currency, and datetime processing.
    • Dynamic Updates: Supports adding languages dynamically online and patching language packs after release to fix translation errors.
    • Automation: Supports automatic translation of extracted text and Babel plugins for automatic t function imports.
    • High Reliability: Over 90% test coverage for the core @voerkai18n/runtime and full TypeScript support.
  3. Overview of VoerkaI18n features

    master

    VoerkaI18n is a framework-agnostic internationalization solution designed for JavaScript/TypeScript applications. Key capabilities include:

    • Framework Agnostic: Works with Vue, React, Svelte, SolidJS, and others.
    • Automatic Extraction: Scans your code to extract translatable text automatically.
    • AI Translation: Integrates with AI services for automated translation.
    • Flexible Formats: Supports various translation methods and file formats.
    • TypeScript Support: Provides full type safety for developers.
  4. Use the @VoerkaI18n/cli for i18n workflows

    master

    The @VoerkaI18n/cli package provides command-line tools to manage internationalization (i18n) workflows. It is used to perform the following core tasks:

    • Initialization: Setting up the i18n configuration and project structure.
    • Text Extraction: Scanning your source code to find and extract translatable text strings.
    • Compilation: Compiling i18n resources into the required formats for your application runtime.
  5. What is VoerkaI18n and when to use it

    master

    VoerkaI18n is a comprehensive internationalization (i18n) solution designed to solve common friction points in existing JavaScript i18n libraries (like i18next or react-intl).

    Key Advantages

    • Intuitive Source Code: Unlike traditional solutions that require manual key management (e.g., $t("message.login")), VoerkaI18n allows you to use natural language directly in your source code (e.g., t("中华人民共和国万岁")). The framework handles the extraction and translation mapping automatically.
    • Monorepo & Multi-library Support: It provides seamless language switching linkage across multiple packages in a monorepo. This allows independent libraries to be developed separately while automatically synchronizing their language state with the main application.
    • Full Engineering Lifecycle: It supports the entire i18n workflow: text extraction $\rightarrow$ automatic translation (via online APIs) $\rightarrow$ compilation $\rightarrow$ dynamic switching.
    • Extensibility: Includes powerful interpolation for plurals, dates, and currencies, and supports dynamic language addition and online language patches after deployment.

    Target Environments

    VoerkaI18n is applicable to any JavaScript application, including:

    • Node.js
    • Vue
    • React
    • ReactNative
  6. How namespaces work during extraction and compilation

    master

    Namespaces are a tool for organizing translation source files during the development and extraction phase.

    1. Extraction Phase: When you run voerkai18n extract, the tool scans the directories specified in your languages/settings.json namespaces configuration. Instead of a single file, it creates separate JSON files in languages/translates/messages/ named after your namespace keys (e.g., routes.json, auth.json).

    2. Compilation Phase: When you run voerkai18n compile, the tool automatically merges all these namespace-specific JSON files back into a single structure. This means the concept of namespaces is transparent to the application at runtime; the application consumes the merged output as if no namespaces were used.

  7. Important notes for React integration

    master

    When using @voerkai18n/react, keep these behaviors in mind:

    • Provider Requirement: You must wrap your application with VoerkaI18nProvider.
    • Re-rendering: To ensure components re-render automatically when the language changes, use the useTranslate hook or the Translate component.
    • Static Functions: If you use a t function imported directly from your language configuration file (e.g., import { t } from './languages'), it returns a string but will not trigger a re-render when the language changes. You must manually manage updates in those cases.
  8. Extract paragraphs using <Translate> components

    master

    For larger blocks of text, use the <Translate> component. The extraction tool identifies these components by their id attribute. Extracted paragraphs are saved as HTML files in languages/translates/paragraphs/ named after the component's id. The output file includes metadata comments (id, scope, file, and range) and wraps the content in div tags with language attributes.

    <template>
        <div>       
            <Translate id="license">
    Copyright (c) [year] [copyright holder]
    ...
            </Translate>
        </div>
    </template>

    Output format (languages/translates/paragraphs/license.html):

    <!-- 
        id:    license 
        scope:  
        file:  src\License\index.vue 
        rang:  3:8-8:12 
    -->
    <div language="zh-CN">
    版权所有 (c) [年份] [版权持有者]...
    </div>
    <div language="en-US">
    Copyright (c) [year] [copyright holder]...
    </div>
  9. Understand the Astro Project Structure

    master

    The project follows a standard Astro directory layout:

    • src/pages/: Contains .astro or .md files. Each file name corresponds to a route in your application.
    • src/content/: Houses 'collections' of Markdown and MDX documents. You can use the getCollection() function to retrieve these documents (e.g., from src/content/blog/) and apply type-checking via schemas.
    • src/components/: A directory for reusable Astro, React, Vue, Svelte, or Preact components.
    • src/layouts/: Contains layout components used to wrap pages.
    • public/: Stores static assets like images that should be served as-is.
    • astro.config.mjs: The configuration file for the Astro environment.
    ├── public/
    ├── src/
    │   ├── components/
    │   ├── content/
    │   ├── layouts/
    │   └── pages/
    ├── astro.config.mjs
    ├── README.md
    ├── package.json
    └── tsconfig.json
  10. Use Text Mapping to reduce bundle size

    master
    When the @voerkai18n/plugins is enabled, it automatically scans source files (like .vue, .js, etc.) and converts t("actual text") calls into t("<id>") based on the mapping table found in your idMap.js file. This process reduces the size of your source code files without affecting internationalization functionality.
  11. Understand the role of @voerkai18n/runtime

    master

    @voerkai18n/runtime is the core runtime dependency required by the main voerkai18n package. While you typically interact with the high-level voerkai18n package, @voerkai18n/runtime provides the underlying execution logic at runtime.

    Key details:

    • It is published in multiple transpiled formats to ensure compatibility: commonjs, esm, and iife.
    • Best Practice: Always keep @voerkai18n/runtime updated to the latest version to ensure compatibility with the main voerkai18n package and to receive the latest runtime improvements.
  12. Determine language configuration precedence

    master

    In a multi-library scenario, the language configuration of the entire application is governed by the i18nScope instances where library is set to false.

    • An application should ideally have only one i18nScope with library: false.
    • If multiple i18nScopes with library: false exist, the last one defined takes precedence for the application's language configuration.