rezoner / playground

Playground.js is a framework for your javascript based games. It gives you out-of-box access to essentials like mouse, keyboard, sound and well designed architecture that you can expand to your needs.
MIT License
469 stars 49 forks source link

Dictionary #27

Open rezoner opened 8 years ago

rezoner commented 8 years ago

Letting you know that I need multi language dictionary for my next game so I am working on one.

Leaving this open for discussion in case you expect some features.

Things so far:

Basic usage


/* dictionary.json */

{ 
  "reportDamage": "Your health is {health}"
}

/* app.js */

app.dictionary("reportDamage", { health: 70 });

> Your health is 70

Tree

{
  "items": {
    "weapon": "bron",
    "shield": "tarcza",
    "armor": "zbroja"
  }
}

app.dictionary("items/shield");

> tarcza

Access object properties

{
  "weaponDescription": "Deals {weapon.damage} damage each {weapon.cooldown} seconds"
}

app.dictionary("weaponDescription", {
  weapon: { damage: 4, cooldown: 6 }
});

> Deals 4 damage each 6 seconds

Self lookup and {nested{tags}}

{tags} are resolved in order from inside to outside

If tag is a slash separated path ex. "{path/to/something}" the tag will be replaced by dictionary entry If tag is a dot separated path ex. "{path.to.something}" the tag will be replaced by replace entry

Hence it is possible to create quite flexible dictionary entries

{
  "descriptions": {
    "weapon": "That is a weapon.",
    "shield": "It is some shield.",
    "armor": "That must be an armor."
  },

  "details": {
    "weapon": "Damage: {damage}"
  }, 

  "look": "Your are looking at the object. {descriptions/{type}} - {details/{type}}"
}

app.dictionary("look", { type: "weapon", "damage": 4 });

> You are looking at the object. That is a weapon. Damage: 4

Pulling random element from dictionary group

If the {tag} path ends with slash it will pull random entry from a group

{

  "insluts": [
    "you rebel scum",
    "you filthy hamster",
    "you worthless bum"
  ],

  "deathWish": "Die, {insults/}"

}

app.dictionary("deathWish");

> Die, you filthy hamster
rezoner commented 8 years ago

First concerns

Path resolving will probably change form from {path/to/entry} to {@path/to/entry} - that is because I want to introduce math mode Your dps is {damage / cooldown} which may also evolve into little less readable but safer form {{damage} / {cooldown}}

That is to give translator as much flexibility as possible (I value mod community more than translators per se)

There is also need for functions like upperCaseFirst()

luizbills commented 8 years ago

I suggest {path.to.entry} instead of {@path/to/entry} or {path/to/entry}

{
  "items": {
    "weapon": "bron",
    "shield": "tarcza",
    "armor": "zbroja"
  }
}

app.dictionary("items.shield");

> tarcza
{
  "weapon": {
    "description": "Deals {item.damage} damage each {item.cooldown} seconds"
  }
}

app.dictionary("weapon.description", {
  item: { damage: 4, cooldown: 6 }
});

> Deals 4 damage each 6 seconds