0
0
Fork 0
mirror of https://gitlab.com/Oreolek/salet-module.git synced 2024-06-26 03:50:49 +03:00

Internationalization

This commit is contained in:
Alexander Yakovlev 2016-09-10 12:48:33 +07:00
parent 9e7a3f0f1c
commit 757c12e2dd
3 changed files with 35 additions and 41 deletions

View file

@ -1,6 +1,6 @@
{
"name": "salet",
"version": "1.3.3",
"version": "1.3.4",
"description": "A general client-side framework for cybertext interactive fiction games.",
"keywords": ["ifiction", "interactive fiction", "games", "coffee-script", "text", "menu"],
"homepage": "http://salet.oreolek.ru",

View file

@ -1,54 +1,50 @@
# Internationalization support
languages = {}
# 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
en = {
terrible: "terrible",
poor: "poor",
mediocre: "mediocre",
fair: "fair",
good: "good",
great: "great",
superb: "superb",
yes: "yes",
no: "no",
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_situation: "You can't move to an unknown situation: {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}'."
}
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_situation: "You can't move to an unknown situation: {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}'."
# Set this data as both the default fallback language, and the english preferred language.
languages[""] = en
languages["en"] = en
push: (lang, strings) ->
if @strings[lang]?
@strings[lang] = merge @strings[lang], strings
else
@strings[lang] = strings
languageCodes = Object.keys(languages)
localize = (languageCode, message) ->
for thisCode in languageCodes
if languages[languageCode]?
localized = languages[languageCode][message]
localize: (message, languageCode = @lang) ->
if @strings[languageCode]?
localized = @strings[languageCode][message]
if localized
return localized
return message
return message
window.i18n = new Localize
# API
String.prototype.l = (args) ->
# Get lang attribute from html tag.
lang = document.getElementsByTagName("html")[0].getAttribute("lang") || ""
# Find the localized form.
localized = localize(lang, this)
localized = window.i18n.localize(this)
if typeof(localized) == "function"
localized = localized(args)
else # Merge in any replacement content.

View file

@ -30,8 +30,6 @@ class Salet
@rnd = null
@time = 0
@languages = languages
# Corresponding room names to room objects.
@rooms = {}