Skip to content

activist-org/i18n-check

Repository files navigation

i18n check logo

issues python pypi pypistatus license coc matrix

Check i18n/L10n keys and values

i18n-check is a Python package to automate the validation of keys and values of your internationalization and localization processes.

Developed by the activist community, this process is meant to assure that development and i18n/L10n teams are in sync when using JSON based localization processes. The action can be expanded later to work for other file type processes as needed.

Note

For the GitHub action please see activist-org/i18n-check-action.

Contents

Conventions

activist i18n keys follow the following conventions that are enforced by i18n-check:

  • All key base paths should be the file path where the key is used
  • If a key is used in more than one file, then the lowest common directory followed by _global is the base path
  • Base paths should be followed by a minimally descriptive content reference
    • Only the formatting of these content references is checked via i18n-check
  • Separate base directory paths by periods (.)
  • Separate all directory and file name components as well as content references by underscores (_)
  • Repeat words in file paths for sub directory organization should not be repeated in the key

Note

An example valid key is:

Key: "components.component_name.CONTENT_REFERENCE"

File: components/component/ComponentName.ext

How it works

Arguments

You provide i18n-check with the following arguments:

  • src-dir: The path to the directory that has source code to check
  • i18n-dir: The directory path to your i18n files
  • i18n-src: The name of the i18n source file
  • i18n-map: The path to the i18n-map file (optional - see below)

Checks

From there the following checks are ran across your codebase:

  • key_identifiers: Does the source file have keys that don't match the above format or name conventions?
    • Rename them so i18n key usage is consistent and their scope is communicated in their name.
  • unused_keys: Does the source file have keys that are not used in the codebase?
    • Remove them so the localization team isn't working on strings that aren't used.
  • non_source_keys: Do the target locale files have keys that are not in the source file?
    • Remove them as they won't be used in the application.
  • repeat_values: Does the source file have repeat values that can be combined into a single key?
    • Combine them so the localization team only needs to localize one of them.
  • map_object: Do the i18nMap object keys match the i18n-src keys.
    • Make sure that the key map object is up to date if using i18nMap (see below).

Each of the above checks is ran in parallel with directions for how to fix the i18n files being provided when errors are raised. Checks can also be disabled in the workflow via options passed in the configuration YAML file.

i18n Object

i18n-check can also generate an i18nMap object from the i18n-src file that can be used to load in i18n keys. Keys within this object map to keys within the source file as in the following example:

// _global.hello_global would be a key in our i18n-src file.
export const i18nMap = {
  _global: {
    hello_global: "_global.hello_global",
  },
};

The map is then used in files in the following way:

// Using vue-i18n:
const hello_global_i18n_scope = i18n.t(i18nMap._global.hello_global);

Using i18nMap allows development teams to check the existence of all i18n keys used in the codebase as linting will detect that keys don't exist on the object. The package will check that i18nMap is up to date with the i18n-src file if this functionality is enabled in the configuration file.

Configuration

The following details the .18n-check.yaml configuration file, with a further example being the configuration file for this repository that we use in testing.

src-dir: frontend
i18n-dir: frontend/i18n
i18n-src: frontend/i18n/en.json
i18n-map: frontend/types/i18n-map.ts

checks:
  - key_identifiers: true
  - unused_keys: true
  - non_source_keys: true
  - repeat_values: true
  - map_object: true

file_types_to_check: [.ts, .js]
directories_to_skip: [frontend/node_modules]
files_to_skip: []
warn_on_nested_i18n_src: true

Common additional arguments for using specific web frameworks can be found in the dropdowns below:

Vue.js

file_types_to_check: [.vue]
directories_to_skip: [.nuxt, .output]

Contributors

Thanks to all our amazing contributors! ❤️