go-i18n

repository·main·Indexed 26 days ago

https://github.com/nicksnyder/go-i18n

A Go package and CLI tool for internationalization (i18n) in Go applications. It supports complex CLDR pluralization rules, template-based variable injection, and multiple message file formats including TOML, JSON, and YAML. The accompanying goi18n CLI provides commands to extract messages from source code and merge translations between active and translation files.

Tokens
1.3K
Snippets
4
Records
11
Agent score
85%

What's inside go-i18n

  1. Upgrade CLDR plural data

    main

    To update the CLDR (Common Locale Data Repository) plural data used by the project, follow these steps:

    1. Visit the Unicode CLDR releases page to identify the latest release and download the source code.
    2. Unzip the downloaded source.
    3. Locate the file common/supplemental/plurals.xml within the unzipped content.
    4. Copy plurals.xml into the internal/plural/codegen directory.
    5. Execute the generate.sh script to regenerate the code.
  2. Translate a new language with goi18n

    main

    To add a new language to your project, follow these steps:

    1. Create an empty file for the target language (e.g., translate.es.toml).
    2. Populate the file with existing messages from your base language (e.g., active.en.toml) using the merge command:
      goi18n merge active.en.toml translate.es.toml
    3. Translate the content in translate.es.toml.
    4. Rename the translated file to its active name (e.g., active.es.toml).
    5. Load the new active.es.toml file into your i18n.Bundle in your Go code.
  3. Update translations for new messages

    main

    When you add new messages to your Go code, follow this workflow to update all language files:

    1. Extract: Run goi18n extract to update your base language file (e.g., active.en.toml).
    2. Merge to translation files: Run goi18n merge active.*.toml to generate updated translate.*.toml files containing the new keys.
    3. Translate: Translate the new entries in the translate.*.toml files.
    4. Merge back to active files: Run goi18n merge active.*.toml translate.*.toml to merge the translations into your production active.*.toml files.
  4. Customize template data and locale in the example project

    main

    The example project allows you to customize template data and the active language using URL query parameters.

    • Use parameters like name or unreadEmailCount to inject data into templates.
    • Use the lang parameter to switch the locale (e.g., lang=es for Spanish).
  5. Use the i18n package for message lookup

    main
    The i18n package allows you to look up messages based on locale preferences. The workflow involves creating a Bundle to hold translations, loading message files into it, and then using a Localizer to retrieve specific messages for a user's language preference.
  6. Manage translation workflow with goi18n

    main

    Follow this workflow to manage translations:

    1. Extract messages: Run goi18n extract to generate a base message file (e.g., en.toml) from your Go code.

      Example en.toml structure:

      [PersonCats]
      description = "The number of cats a person has"
      one = "{{.Name}} has {{.Count}} cat."
      other = "{{.Name}} has {{.Count}} cats."
    2. Prepare for translation: Run goi18n merge to create a template file for translators (e.g., translate.es.toml). This file includes a hash to track message changes.

      Example translate.es.toml structure:

      [PersonCats]
      description = "The number of cats a person has"
      hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
      one = "{{.Name}} has {{.Count}} cat."
      other = "{{.Name}} has {{.Count}} cats."
    3. Merge translations: Once translated, run goi18n merge to merge the translations into your active message file (e.g., active.es.toml).

      Example active.es.toml structure:

      [PersonCats]
      description = "The number of cats a person has"
      hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
      one = "{{.Name}} tiene {{.Count}} gato."
      other = "{{.Name}} tiene {{.Count}} gatos."
  7. Load TOML messages into a bundle

    main

    To use your generated TOML message files in your Go application, register the TOML unmarshaler with your bundle and load the active message file.

    bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
    bundle.MustLoadMessageFile("active.es.toml")
  8. Use goi18n commands

    main

    The goi18n tool provides two primary subcommands for managing translation workflows:

    • extract: Scans your Go source files to create a message file containing all defined messages.
    • merge: Used for two purposes:
      1. Creating message files intended for translation (e.g., translate.es.toml).
      2. Merging translated message files back into your active message files (e.g., active.es.toml).
    goi18n command [arguments]
    
    Commands:
      merge     merge message files
      extract   extract messages from Go files