Skip to content

Generate Lingui ID with URL and filename safe alphabet #2207

@jimmymcpeter

Description

@jimmymcpeter

Problem Description

My translation system's key names support pretty much anything. I'm able to import format-json files without issue. One challenge I've encountered however relates to the generated Lingui IDs using the standard base64 encoding alphabet--

export function generateMessageId(msg: string, context = "") {
return hexToBase64(sha256(msg + UNIT_SEPARATOR + (context || ""))).slice(0, 6)
}

The 62nd and 63rd characters in the base 64 alphabet are + and / and a lot of my generated IDs contain these -- https://datatracker.ietf.org/doc/html/rfc4648#section-4

If my translations need to subsequently get sent to a different vendor/translation system, it can fail if XLIFF v2.0 is used. The id element type in the XLIFF 2.0 schema is xs:NMTOKEN. This means the value may only consist of letters, digits, periods, hyphens, underscores, and colons. No whitespace is allowed -- https://www.datypic.com/sc/xsd/t-xsd_NMTOKEN.html

Proposed Solution

It would be nice if an option was added to generate Lingui ID's using the URL and filename safe Base 64 alphabet.
The 62nd and 63rd characters would instead be - and _ -- https://datatracker.ietf.org/doc/html/rfc4648#section-5

Below is an example overriding the current generateMessageId function --

import crypto from 'node:crypto';

const UNIT_SEPARATOR = "\u001F"

export function generateMessageId(msg: string, context = "") {
  const result = crypto.createHash('sha256');

  result.write(msg + UNIT_SEPARATOR + (context || ""));

  result.end();

  return result.digest('base64url').slice(0, 6);
}

Alternatives Considered

A custom formatter is necessary to work around this.

Additional Context

Kudos on such a great library. I've worked on quite a few i18n/l10n projects, and it's been really refreshing to work on one that uses Lingui.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions