{"id":19592317,"url":"https://github.com/smikhalevski/mfml","last_synced_at":"2025-04-27T14:33:40.521Z","repository":{"id":42659487,"uuid":"376873048","full_name":"smikhalevski/mfml","owner":"smikhalevski","description":"The ICU MessageFormat + XML/HTML compiler and runtime that makes your translations tree-shakeable.","archived":false,"fork":false,"pushed_at":"2022-03-29T14:41:37.000Z","size":1858,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T00:00:08.468Z","etag":null,"topics":["compiler","html","i18n","icu","intl","l10n","language","message-format","parser","tms","translation","xml"],"latest_commit_sha":null,"homepage":"https://smikhalevski.github.io/mfml/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/smikhalevski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-14T15:34:07.000Z","updated_at":"2024-07-23T15:27:42.000Z","dependencies_parsed_at":"2022-09-26T20:01:03.434Z","dependency_job_id":null,"html_url":"https://github.com/smikhalevski/mfml","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smikhalevski%2Fmfml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smikhalevski%2Fmfml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smikhalevski%2Fmfml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smikhalevski%2Fmfml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smikhalevski","download_url":"https://codeload.github.com/smikhalevski/mfml/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251154710,"owners_count":21544547,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compiler","html","i18n","icu","intl","l10n","language","message-format","parser","tms","translation","xml"],"created_at":"2024-11-11T08:34:37.118Z","updated_at":"2025-04-27T14:33:38.929Z","avatar_url":"https://github.com/smikhalevski.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mfml [![build](https://github.com/smikhalevski/mfml/actions/workflows/master.yml/badge.svg?branch=master\u0026event=push)](https://github.com/smikhalevski/mfml/actions/workflows/master.yml)\n\nThe ICU MessageFormat + XML/HTML compiler and runtime that makes your translations tree-shakeable.\n\n# Motivation\n\nSplitting the app code into chunks and loading those chunks at runtime is usually handled by a bundler. Modern i18n\ntools, such as `i18next`, rely on JSON files that are baked into an application bundle or loaded asynchronously. This\napproach has several downsides:\n\n- You have to somehow split your translations into separate JSON files before bundling. Otherwise, your clients would\n  have to download all translations at application start;\n- You have to load translations manually when they are needed;\n- Non-used translations are still bundled and loaded unless you manually delete them;\n- Errors, such as missed interpolation parameters and illegal parameter types, are thrown at runtime.\n\nAll the above is highly error-prone due to the human factor.\n\nMFML compiles translations into message functions that you can import as a module into your code. This approach provides\nmultiple benefits:\n\n- Message functions are bundled and loaded along with your code. So you don't have to manage the translation loading\n  process manually;\n- Message functions are tree-shakeable, so chunks assembled by a bundler would contain only used translations;\n- Message functions can be type-checked at compile time. So you won't forget that translation required a parameter, or a\n  parameter must be of a particular type. Change in the translation key would require changes in modules that import a\n  corresponding message function. Otherwise, a type checker, or a bundler would signal you that the imported function is\n  missing;\n- Message functions use a runtime to render the translation. Runtimes can produce React elements, plain strings, or any\n  other output you need.\n\nMFML supports ICU MessageFormat and XML/HTML markup. The compiler is highly customizable, and the runtime\nis [just 3 kB gzipped](https://bundlephobia.com/result?p=@mfml/runtime).\n\n# Usage example\n\nInstall compiler, CLI utility and runtime libraries.\n\n```shell\nnpm install --save-prod @mfml/runtime \n\nnpm install --save-dev @mfml/compiler @mfml/cli \n```\n\nCreate files that hold your translations. File names must be locale names. By default, locales are matched\nusing [`locale-matcher`](https://github.com/smikhalevski/locale-matcher), so `en-US`, `en_us`, `en`, and\neven `HE/il-u-ca-hebrew+tz/jeruslm` are all valid locale names.\n\n```json5\n// ./translations/en-US.json\n\n{\n  \"sayHello\": \"Hello, \u003cstrong\u003e{name}\u003c/strong\u003e\"\n}\n```\n\n```json5\n// ./translations/ru-RU.json\n\n{\n  \"sayHello\": \"Привет, \u003cstrong\u003e{name}\u003c/strong\u003e\"\n}\n```\n\nCompile these files with `mfmlc` to TypeScript. Or remove `--ts` flag to disable typings.\n\n```shell\nmfmlc --include './translations/*.json' --outDir './gen' --ts;\n```\n\nThis command would output `./gen/messages.ts` file with message functions and corresponding TypeScript interfaces.\n\n\u003cdetails\u003e\n\u003csummary\u003eThe content of \u003ccode\u003e./gen/messages.ts\u003c/code\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```ts\nimport {MessageFunction} from '@mfml/runtime';\n\nconst b = 'en-US';\nconst d = [b, 'ru-RU'];\n\nexport interface SayHelloValues {\n  name: unknown;\n}\n\nlet sayHello: MessageFunction\u003cSayHelloValues\u003e = (runtime, locale, values) =\u003e {\n  const {f, e, a, l} = runtime;\n  const {name: g} = values;\n  return l(locale, d) === 1\n      ? f('Привет, ', e('strong', null, a(locale, g)))\n      : f('Hello, ', e('strong', null, a(b, g)));\n};\n\nexport {sayHello};\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\nImport compiled message functions in your code.\n\n```ts\nimport {sayHello} from './gen/messages';\nimport {createStringRuntime} from '@mfml/runtime';\n\nconst stringRuntime = createStringRuntime();\n\nconsole.log(sayHello(stringRuntime, 'en-US', {name: 'Karen'})); // → \"Hello, Karen!\"\n```\n\nHave a look at https://github.com/smikhalevski/mfml/tree/master/example for real-world use-case implementation.\n\n# Configuration\n\nConfigurations have the following priorities from highest to lowest:\n\n- CLI parameters;\n- Options from `mfml.config.js` or other configuration file provided through `--config` CLI parameter;\n- Options from presets.\n\n## CLI parameters\n\n##### `-h`/`--help`\n\nPrint the help document.\n\n##### `-V`/`--version`\n\nPrint the installed version number.\n\n##### `-c \u003cfile\u003e`, `--config \u003cfile\u003e`\n\nThe configuration file path. By default, `mfmlc` looks up for a `mfml.config.js` in the current directory.\n\n##### `-i \u003cpattern\u003e`, `--include \u003cpattern\u003e`\n\nThe file paths' pattern that must be included in the compilation.\nSupports [glob expressions](https://github.com/isaacs/node-glob).\n\nThis option can be specified multiple times.\n\nThis option is required and must present either among CLI options or in a configuration file.\n\n##### `-o \u003cdir\u003e`, `--outDir \u003cdir\u003e`\n\nThe output folder for all emitted files. This option is required and must present either among CLI options or in a\nconfiguration file.\n\n##### `-d \u003cdir\u003e`, `--rootDir \u003cdir\u003e`\n\nThe root folder from which included file paths are resolved.\n\n##### `-p \u003cpreset\u003e`, `--preset \u003cpreset\u003e`\n\nThe name of the built-in preset ([`xml`](./packages/cli/src/main/presets/xml.ts) and\n[`html`](./packages/cli/src/main/presets/html.ts)) or path to a module that exports a configuration.\n\n```shell\nmfmlc --inlcude './translations/*.json' --outDir './gen' --preset html\n```\n\nThis option can be specified multiple times.\n\n##### `-a \u003cadapter\u003e`, `--adapter \u003cadapter\u003e`\n\nThe name of the built-in adapter ([`localeFilesAdapter`](./packages/cli/src/main/adapters/localeFilesAdapter.ts))\nor a path to a module that exports the adapter function.\n\n##### `-l \u003clocale\u003e`, `--defaultLocale \u003clocale\u003e`\n\nThe default locale. If omitted then the first locale in message would be used as a default.\n\n##### `-t`/`--ts`\n\nProduce TypeScript typings for message functions.\n\n## Configuration files\n\n[The full list of options supported in a configuration file.](https://smikhalevski.github.io/mfml/interfaces/cli_src_main.IConfig.html)\n\n# React integration\n\nThe `@mfml/react-runtime` provides the runtime that renders translations as React nodes and a couple of handy components\nand utilities.\n\nLet's assume we've compiled translations from [the original example](#usage-example). Then we can use then im React\nmarkup like this:\n\n```tsx\nimport {sayHello} from './gen/messages';\n\n\u003cMessage\n    message={sayHello}\n    values={{name: 'Karen'}}\n/\u003e\n```\n\nYou can use a hook to render a message:\n\n```ts\nimport {useEffect} from 'react';\nimport {useMessage} from '@mfml/react-runtime';\nimport {sayHello} from './gen/messages';\n\nconst MyAlert = () =\u003e {\n  const t = useMessage();\n\n  useEffect(() =\u003e {\n    alert(t(sayHello, {name: 'Karen'}));\n  }, []);\n\n  return null;\n};\n```\n\nHave a look at https://github.com/smikhalevski/mfml/tree/master/example for real-world use-case implementation.\n\n# ICU considerations\n\nBy default, the parser uses the\ndefault `DOUBLE_OPTIONAL` [apostrophe mode](http://site.icu-project.org/design/formatting/messageformat/newsyntax), in\nwhich a single apostrophe only starts quoted literal text if it immediately precedes a curly brace `{}`, or a pound\nsymbol `#` if inside a plural format. A literal apostrophe `'` is represented by either a single `'` or a doubled `''`\napostrophe character.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmikhalevski%2Fmfml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmikhalevski%2Fmfml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmikhalevski%2Fmfml/lists"}