mirror of
https://gitlab.com/Oreolek/salet-module.git
synced 2024-07-02 23:05:02 +03:00
57 lines
1.9 KiB
CoffeeScript
57 lines
1.9 KiB
CoffeeScript
# Internationalization support
|
|
|
|
# object merging utility function
|
|
merge=(xs...) ->
|
|
if xs?.length>0
|
|
tap {},(m)->m[k]=v for k,v of x for x in xs
|
|
tap=(o, fn)->fn(o);o
|
|
|
|
class Localize
|
|
# Default Messages
|
|
constructor: (lang = "en") ->
|
|
@lang = document.getElementsByTagName("html")[0].getAttribute("lang") || lang
|
|
|
|
strings:
|
|
en:
|
|
choice: "Choice {number}",
|
|
no_group_definition: "Couldn't find a group definition for {id}.",
|
|
link_not_valid: "The link '{link}' doesn't appear to be valid.",
|
|
link_no_action: "A link with a situation of '.', must have an action.",
|
|
unknown_room: "Room not found: {id}.",
|
|
existing_situation: "You can't override situation {id} in HTML.",
|
|
erase_message: "This will permanently delete this character and immediately return you to the start of the game. Are you sure?",
|
|
no_current_situation: "I can't display, because we don't have a current situation.",
|
|
no_local_storage: "No local storage available.",
|
|
random_seed_error: "You must provide a valid random seed.",
|
|
random_error: "Initialize the Random with a non-empty seed before use.",
|
|
dice_string_error: "Couldn't interpret your dice string: '{string}'."
|
|
|
|
push: (lang, strings) ->
|
|
if @strings[lang]?
|
|
@strings[lang] = merge @strings[lang], strings
|
|
else
|
|
@strings[lang] = strings
|
|
|
|
localize: (message, languageCode = @lang) ->
|
|
if @strings[languageCode]?
|
|
localized = @strings[languageCode][message]
|
|
if localized
|
|
return localized
|
|
return message
|
|
|
|
window.i18n = new Localize
|
|
|
|
# API
|
|
String.prototype.l = (args) ->
|
|
# Find the localized form.
|
|
localized = window.i18n.localize(this)
|
|
if typeof(localized) == "function"
|
|
localized = localized(args)
|
|
else # Merge in any replacement content.
|
|
if args
|
|
for name in args
|
|
localized = localized.replace(
|
|
new RegExp("\\{"+name+"\\}"), args[name]
|
|
)
|
|
return localized
|