Skip to content

Instantly share code, notes, and snippets.

@labithiotis
Last active August 29, 2015 14:28
Show Gist options
  • Save labithiotis/f4f4d3fd9fec28c0e8a1 to your computer and use it in GitHub Desktop.
Save labithiotis/f4f4d3fd9fec28c0e8a1 to your computer and use it in GitHub Desktop.
Specification for locale tempting
LOCALE SPEC
Parse string and inject appropriate variables,
process through each {{ }},
determine each type and return variable.
Types and Options have long and short distinguishes.
TYPES:
gender (g) | Male or Female
integer (i) | 1,2,3,4 etc..
number (n) | one, two, three, four etc..
ordinal (oi) | 1st, 2nd, 3rd, 4th, etc..
ordinal (on) | first, second, thrid etc..
TYPES OPTIONS:
gender | male (m), female (f)
integer, number | zero (z), one (o), two (t), few (f), many (m), other (x)
VARIABLE:
This can be any valid json expression from json passed in:
data.profile.gender
> male
If template just contains {{VARAIBLE}} it will just replace it with varaible. Maybe we can determin this by absense of $ in between {{}}
EXAMPLES
TEXT {{$TYPE $VARIABLE $TYPE_OPTION{TEXT}}} TEXT
{{$g $variable $m{He} $f{She}}} posted {{$i $variable $one{ comment} $other{ comments}}}
> She posted 1 comment
> He posted 10 comments
On amazon they have {{$n $variable $one{ book} $other{ books}}}
> On amazon they have one hundred books
> On amazon they have one book
Should also accept nested expressions:
{{$g $variable $m{He posted {{$n $variable $one{ comment} $other{ comments}}}} $f{She {{$n $variable $one{ comment} $other{ comments}}}}}}
> She posted 1 comment
> He posted 10 comments
// Should it handle ordinals or let a lib like numberal.js do it?
Ordinal for ints
function(num) {
var b = num % 10;
return 1 === ~~(num % 100 / 10) ? "th" : 1 === b ? "st" : 2 === b ? "nd" : 3 === b ? "rd" : "th"
}
Ordinal for words will require each one, base 10
1: { 1: "first", 2: "second", 3: "third" ..... 11: "evelen", 12: "twelve" }
10: { 1: null, 2: "twenty", 3: "thirty", 4: "forty" }
100: { 1: hundred }
1000: { 1: thousand }
Combine above with null rules to step down:
1.1 1000.1 and 1.2 1.2
> one thousdand and twenty two
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment