Skip to content

More powerful translation system #55

@Sv443

Description

@Sv443

Create a library with an extended syntax for declaring translations.
The goal is not to be as flexible as the other translation libs currently in existence, it's to be as lightweight as possible for userscript usage, while being a step above UserUtils' current, VERY simple translation system.

Requirements:

  • Support for named variables (numbered ones are kinda wack for context purposes)
  • Support for nested translations using dot syntax (i.e. generic.buttons.submit)
  • Support for arbitrary transformation functions

Nice to have:

  • Interpret certain translations as raw HTML, others as a string (XSS prevention)
  • Support for calling arbitrary actions that result in a string, using passed parameters
Example - click to expand
{
  // regular translation with indexed parameters - like it is right now
  "example0": "My translation blabla, %0",

  // translation treated as raw HTML, with a localized URL
  "example1.0": "[/];My translation blabla <a href=\"local url\">%0</a>",
  // translation treated as raw HTML, with a variable URL provided by the implementer
  "example1.1": "[/];My translation blabla <a href=\"%0\">text</a>",
  // translation with named variables and indexed parameters at the same time
  "example2": "[$];My translation blabla ${namedVar}, %0",
  // translation with an action call that results in an arbitrary string
  "example3": "[@];My translation blabla @{actionName,%0,param2}",

  // combination of all the above
  "example4": "[/$@];My translation blabla <a href=\"%0\">${namedVar}</a> @{actionName,%1,param2}"
};

Example usage:

tr("example0", ["param1"]);

tr("example1.0", ["anchor text"]);

tr("example1.1", ["https://example.com"]);

tr("example2", { namedVar: "namedVar", 0: "param1" });

tr.defineAction("actionName", (locale, ...params) => {
  return "action result";
});
tr("example3", ["param1"]);

tr("example4", { namedVar: "namedVar", 0: "https://example.com", "1": "param2" });
// ^ should support stringified number keys too, because of object & array merging

Metadata

Metadata

Assignees

Labels

E2 🟠High level of required effort and potential complicationsenhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions