Skip to main content

JSONTranslator

Utility function that loads a translator from a folder or a table.

To get translations uploaded.

  1. Run the game
  2. On the client, check LocalizationService.GeneratedJSONTable
  3. Right click > Save as CSV
  4. Stop the game
  5. In Studio, go to plugins > "Localization Tools"
  6. Upload the CSV (update)

Functions

new

JSONTranslator.new(
translatorNamestring,--

Name of the translator. Used for source.

...any
) → JSONTranslator

Constructs a new JSONTranslator from the given args.

local translator = JSONTranslator.new("MyTranslator", en", {
	actions = {
		respawn = "Respawn {playerName}";
	};
})

print(translator:FormatByKey("actions.respawn"), { playerName = "Quenty"}) --> Respawn Quenty

-- Observing is preferred
maid:GiveTask(translator:ObserveFormatByKey("actions.respawn", {
	playerName = RxInstanceUtils.observeProperty(player, "DisplayName");
}):Subscribe(function(text)
	print(text) --> "Respawn Quenty"
end)
local translator = JSONTranslator.new(script)
-- assume there is an `en.json` underneath the script with valid JSON.

ObserveLocaleId

JSONTranslator:ObserveLocaleId() → Observable<string>

Observes the current locale id for this translator.

SetEntryValue

JSONTranslator:SetEntryValue(
translationKeystring,
sourcestring,
contextstring,
localeIdstring,
textstring
) → ()

Adds an entry value to the localization table itself. This can be useful for ensuring pseudo localization and/or generating localization values from the game data itself.

GetLocaleId

JSONTranslator:GetLocaleId() → string

Gets the current localeId of the translator if it's initialized, or a default if it is not.

GetLocalizationTable

JSONTranslator:GetLocalizationTable() → LocalizaitonTable

Gets the localization table the translation is using.

PromiseLoaded

JSONTranslator:PromiseLoaded() → Promise

Returns a promise that will resolve once the translator is loaded from the cloud.

FallbackTo

JSONTranslator:FallbackTo(translatorJSONTranslator | Translator) → ()

Makes the translator fall back to another translator if an entry cannot be found.

Mostly just used for testing.

PromiseFormatByKey

JSONTranslator:PromiseFormatByKey(
keystring,
argstable?
) → Promise<string>

Formats the resulting entry by args.

tip

You should use JSONTranslator.ObserveFormatByKey instead of this to respond to locale changing.

ObserveFormatByKey

JSONTranslator:ObserveFormatByKey(
keystring,
argDatatable?--

May have observables (or convertable to observables) in it.

) → Observable<string>

Observes the translated value

FormatByKey

JSONTranslator:FormatByKey(
keystring,
argstable?
) → string

Formats or errors if the cloud translations are not loaded.

tip

You should use JSONTranslator.ObserveFormatByKey instead of this to respond to locale changing.

Destroy

JSONTranslator:Destroy() → ()

Cleans up the translator and deletes the localization table if it exists.

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a new JSONTranslator from the given args.\n\n```lua\nlocal translator = JSONTranslator.new(\"MyTranslator\", en\", {\n\tactions = {\n\t\trespawn = \"Respawn {playerName}\";\n\t};\n})\n\nprint(translator:FormatByKey(\"actions.respawn\"), { playerName = \"Quenty\"}) --> Respawn Quenty\n\n-- Observing is preferred\nmaid:GiveTask(translator:ObserveFormatByKey(\"actions.respawn\", {\n\tplayerName = RxInstanceUtils.observeProperty(player, \"DisplayName\");\n}):Subscribe(function(text)\n\tprint(text) --> \"Respawn Quenty\"\nend)\n```\n\n```lua\nlocal translator = JSONTranslator.new(script)\n-- assume there is an `en.json` underneath the script with valid JSON.\n```",
            "params": [
                {
                    "name": "translatorName",
                    "desc": "Name of the translator. Used for source.",
                    "lua_type": "string"
                },
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "JSONTranslator"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 66,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "ObserveLocaleId",
            "desc": "Observes the current locale id for this translator.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Observable<string>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 99,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "SetEntryValue",
            "desc": "Adds an entry value to the localization table itself. This can be useful\nfor ensuring pseudo localization and/or generating localization values\nfrom the game data itself.",
            "params": [
                {
                    "name": "translationKey",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "source",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "context",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "localeId",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "text",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 118,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "GetLocaleId",
            "desc": "Gets the current localeId of the translator if it's initialized, or a default if it is not.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 158,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "GetLocalizationTable",
            "desc": "Gets the localization table the translation is using.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "LocalizaitonTable"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 173,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "PromiseLoaded",
            "desc": "Returns a promise that will resolve once the translator is loaded from the cloud.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 181,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "FallbackTo",
            "desc": "Makes the translator fall back to another translator if an entry cannot be found.\n\nMostly just used for testing.",
            "params": [
                {
                    "name": "translator",
                    "desc": "",
                    "lua_type": "JSONTranslator | Translator"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 192,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "PromiseFormatByKey",
            "desc": "Formats the resulting entry by args.\n\n:::tip\nYou should use [JSONTranslator.ObserveFormatByKey] instead of this to respond\nto locale changing.\n:::",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "args",
                    "desc": "",
                    "lua_type": "table?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<string>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 211,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "ObserveFormatByKey",
            "desc": "Observes the translated value",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "argData",
                    "desc": "May have observables (or convertable to observables) in it.",
                    "lua_type": "table?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Observable<string>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 226,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "FormatByKey",
            "desc": "Formats or errors if the cloud translations are not loaded.\n\n:::tip\nYou should use [JSONTranslator.ObserveFormatByKey] instead of this to respond\nto locale changing.\n:::",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "args",
                    "desc": "",
                    "lua_type": "table?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 276,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "Destroy",
            "desc": "Cleans up the translator and deletes the localization table if it exists.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 358,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "JSONTranslator",
    "desc": "Utility function that loads a translator from a folder or a table.\n\nTo get translations uploaded.\n\n1. Run the game\n2. On the client, check LocalizationService.GeneratedJSONTable\n3. Right click > Save as CSV\n4. Stop the game\n5. In Studio, go to plugins > \"Localization Tools\"\n6. Upload the CSV (update)",
    "source": {
        "line": 15,
        "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
    }
}