Skip to content

Compile only a single translation #328

@jantimon

Description

@jantimon

I really like the idea of precompiling translation keys during build time 👍

The current API is really cool:

const MessageFormat = require("@messageformat/core");
const compileModule = require("@messageformat/core/compile-module");

const messageFormat = new MessageFormat("en");

console.log(
  compileModule(messageFormat, {
    productCount:
      "{ count, plural,\n   =0 {{formattedCount} Products}\n  one {{formattedCount} Product}\nother {{formattedCount} Products} } ",
  })
);

it generates:

import { en } from "@messageformat/runtime/lib/cardinals";
import { plural } from "@messageformat/runtime";
export default {
  productCount: (d) => plural(d.count, 0, en, { "0": d.formattedCount + " Products", one: d.formattedCount + " Product", other: d.formattedCount + " Products" }) + " "
}

Unfortunately we would prefer to compile only a single translation so we would like to get the following output:

import { en } from "@messageformat/runtime/lib/cardinals";
import { plural } from "@messageformat/runtime";
export default (d) => plural(d.count, 0, en, { "0": d.formattedCount + " Products", one: d.formattedCount + " Product", other: d.formattedCount + " Products" }) + " "

Or even more complicated:

import { en } from "@messageformat/runtime/lib/cardinals";
import { de } from "@messageformat/runtime/lib/cardinals";
import { plural } from "@messageformat/runtime";
export default ([
  (d) => plural(d.count, 0, en, { "0": d.formattedCount + " Products", one: d.formattedCount + " Product", other: d.formattedCount + " Products" }) + " ",
  (d) => plural(d.count, 0, de, { "0": d.formattedCount + " Produkte", one: d.formattedCount + " Produkt", other: d.formattedCount + " Produkte" }) + " "
])

Would it be possible to provide 3 public functions e.g. with the following api?

getCardinalImport('en') -> // import { en } from "@messageformat/runtime/lib/cardinals";
getRuntimeImport() -> // import { plural } from "@messageformat/runtime";
getMessageFormater(messageFormat, "{ count, plural,\n   =0 {{formattedCount} Products}\n  one {{formattedCount} Product}\nother {{formattedCount} Products} } ") // -> (d) => plural(d.count, 0, en, { "0": d.formattedCount + " Products", one: d.formattedCount + " Product", other: d.formattedCount + " Products" }) + " "

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions