Skip to main content

MetricUnitTranslator

Translated unit names and symbols, streamed to each client one locale at a time. A JSONTranslator over the <locale>.json files beside this module, so a player downloads only the English source and their own language. Get it from the ServiceBag and pass it to MetricUnit.format or RxMetricUnitFormatUtils.observeFormat.

local translator = serviceBag:GetService(require("MetricUnitTranslator"))

maid:GiveTask(RxMetricUnitFormatUtils.observeFormat(translator, MetricUnit.new(3, "mile"):ToUSCustomary(), {
	unitDisplay = "long",
}):Subscribe(function(text)
	label.Text = text -- "3 miles", "3 millas", "3 Meilen", "3 мили"
end))

Seventeen languages ship, the ones NumberLocalizationUtils formats numbers for: English, Spanish, French, German, Portuguese, Italian, Russian, Polish, Turkish, Indonesian, Vietnamese, Thai, Arabic, Simplified and Traditional Chinese, Japanese and Korean. Every file has every key:

  • units.<unitId>.long.<category> and units.<unitId>.short.<category> for each of CLDR's plural categories (zero, one, two, few, many, other), picked by PluralCategoryUtils.getPluralCategory.
  • per.<timeUnit>.long and per.<timeUnit>.short, where {n} is the whole "number unit" before them.

{n} is the number, written for the locale. The files are maintained by hand. English matches MetricUnitDefinitions, which a spec checks; the other languages come from Unicode CLDR's unit patterns (see the Unicode notice in this module's source). A no-break space joins a number to its unit.

Show raw api
{
    "functions": [],
    "properties": [],
    "types": [],
    "name": "MetricUnitTranslator",
    "desc": "Translated unit names and symbols, streamed to each client one locale at a time. A `JSONTranslator` over the\n`<locale>.json` files beside this module, so a player downloads only the English source and their own language.\nGet it from the [ServiceBag] and pass it to [MetricUnit.format] or [RxMetricUnitFormatUtils.observeFormat].\n\n```lua\nlocal translator = serviceBag:GetService(require(\"MetricUnitTranslator\"))\n\nmaid:GiveTask(RxMetricUnitFormatUtils.observeFormat(translator, MetricUnit.new(3, \"mile\"):ToUSCustomary(), {\n\tunitDisplay = \"long\",\n}):Subscribe(function(text)\n\tlabel.Text = text -- \"3 miles\", \"3 millas\", \"3 Meilen\", \"3 мили\"\nend))\n```\n\nSeventeen languages ship, the ones `NumberLocalizationUtils` formats numbers for: English, Spanish, French,\nGerman, Portuguese, Italian, Russian, Polish, Turkish, Indonesian, Vietnamese, Thai, Arabic, Simplified and\nTraditional Chinese, Japanese and Korean. Every file has every key:\n\n- `units.<unitId>.long.<category>` and `units.<unitId>.short.<category>` for each of CLDR's plural categories\n  (`zero`, `one`, `two`, `few`, `many`, `other`), picked by [PluralCategoryUtils.getPluralCategory].\n- `per.<timeUnit>.long` and `per.<timeUnit>.short`, where `{n}` is the whole \"number unit\" before them.\n\n`{n}` is the number, written for the locale. The files are maintained by hand. English matches\n[MetricUnitDefinitions], which a spec checks; the other languages come from Unicode CLDR's unit patterns (see the\nUnicode notice in this module's source). A no-break space joins a number to its unit.",
    "source": {
        "line": 31,
        "path": "src/metricutils/src/Shared/Localization/MetricUnitTranslator/init.lua"
    }
}