sonic16x / interslavic

Interslavic dictionary
https://interslavic-dictionary.com/
MIT License
50 stars 17 forks source link

refactor: smart transliteration in translate #278

Closed noomorph closed 6 months ago

noomorph commented 6 months ago

This pull request improves the maintainability of transliteration to various Interslavic scripts, e.g.:

"Tutoj kod možete viděti na {GitHub}."

The goal is to preserve substitutions ({GitHub}) while applying appropriate transliteration based on the selected alphabet (Latin, Cyrillic, or Glagolitic).

Introduced utility functions:

  1. parseI18nString: Parses the translation string into an array of strings and substitutions for easier handling.
  2. createTaggedTemplate: Creates a tagged template literal, allowing the application of transliteration functions to static string parts or interpolated values based on the passed transformation function (unknown => string). In our case, that are transliteration functions.

Example of tagged template literals:

const greet = (strings, ...values) => {
  return `${strings[0]}${values[0]}${strings[1]}`;
};

const name = "Alice";
console.log(greet`Hello, ${name}!`); // Output: "Hello, Alice!"

Pros:

Cons:

ru-danko commented 6 months ago

Thanks for reviewing and improving the code