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.

localeIdstring,
dataTabletable
) → 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.

ObserveFormatByKey

JSONTranslator:ObserveFormatByKey(
translationKeystring,
translationArgstable?--

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

) → Observable<string>

Observes the translated value

PromiseFormatByKey

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

Formats the resulting entry by args.

tip

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

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.

FormatByKey

JSONTranslator:FormatByKey(
translationKeystring,
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. Should be called by ServiceBag

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": "localeId",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "dataTable",
                    "desc": "",
                    "lua_type": "table"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "JSONTranslator"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 65,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "ObserveFormatByKey",
            "desc": "Observes the translated value",
            "params": [
                {
                    "name": "translationKey",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "translationArgs",
                    "desc": "May have observables (or convertable to observables) in it.",
                    "lua_type": "table?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Observable<string>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 118,
                "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": "translationKey",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "args",
                    "desc": "",
                    "lua_type": "table?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<string>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 170,
                "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": 193,
                "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": 208,
                "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": 247,
                "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": 256,
                "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": 264,
                "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": "translationKey",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "args",
                    "desc": "",
                    "lua_type": "table?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 280,
                "path": "src/clienttranslator/src/Shared/JSONTranslator.lua"
            }
        },
        {
            "name": "Destroy",
            "desc": "Cleans up the translator and deletes the localization table if it exists.\nShould be called by [ServiceBag]",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 355,
                "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"
    }
}