0
0
Fork 0
mirror of https://gitlab.com/Oreolek/salet-module.git synced 2024-06-16 23:20:54 +03:00

i18n tests, syntax fixes, code lint

- jQuery extend instead of homebrew merge
- i18n tests
- i18n parameter bugfix
- jshint-suggested syntax fixes
This commit is contained in:
Alexander Yakovlev 2016-12-19 19:54:13 +07:00
parent 9c4c7d777e
commit 0fbf82b994
4 changed files with 22 additions and 21 deletions

View file

@ -1,6 +1,6 @@
{
"name": "salet",
"version": "1.6.14",
"version": "1.6.15",
"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,34 +1,21 @@
# 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
# Default Messages
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
@strings[lang] = $.extend @strings[lang], strings
else
@strings[lang] = strings
@ -39,7 +26,7 @@ class Localize
return localized
return message
window.i18n = new Localize
window.i18n = new Localize()
# API
String.prototype.l = (args) ->
@ -49,7 +36,7 @@ String.prototype.l = (args) ->
localized = localized(args)
else # Merge in any replacement content.
if args
for name in args
for name of args
localized = localized.replace(
new RegExp("\\{"+name+"\\}"), args[name]
)

View file

@ -6,7 +6,7 @@ There is only one instance of this class.
###
class Salet
constructor: (spec) ->
@character = new Character
@character = new Character()
# REDEFINE THIS IN YOUR GAME
@game_id = null
@ -384,7 +384,7 @@ class Salet
return
@progress = saveFile.progress
@character = new Character
@character = new Character()
@rnd = new Random(@progress.seed)
@ -405,7 +405,7 @@ class Salet
now = new Date().getTime() * 0.001
startTime = now - @progress.saveTime
@view = new SaletView
@view = new SaletView()
@getSave = () ->
id = @getSaveId()

View file

@ -2,6 +2,13 @@ salet.game_id = "2829f25a-c77a-4957-9662-e32b082f9f05";
salet.game_version = "1.0";
salet.autosave = false;
salet.autoload = false;
i18n.push("en", {
hello: "Hello world!",
func: function(argument) {
return "Hello "+argument;
},
arg: "Hello {word}!"
})
$(document).ready(function() {
salet.beginGame();
@ -12,6 +19,13 @@ $(document).ready(function() {
QUnit.test("Markdown is working", function(assert) {
return assert.equal(markdown("*hi*"), "<p><em>hi</em></p>\n", "Markdown is good");
});
QUnit.test("Translations are good", function(assert) {
assert.equal("hello".l(), "Hello world!", "Translations were imported fine");
assert.equal("func".l("world!"), "Hello world!", "Translations can be functions");
assert.equal("arg".l({word: "world"}), "Hello world!", "Argument substitutions are good");
assert.equal("choice".l({number: 2}), "Choice 2", "Default translations are good");
return;
});
QUnit.test("RNG is working", function(assert) {
assert.ok(salet.rnd.randf() >= 0, "Random float is not negative");
assert.ok(salet.rnd.randf() < 1, "Random float is less than 1");