Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

prototype of i18n features

  • Loading branch information...
commit 6e46196b40ad75e4148528d83894d9e1731d0b5e 1 parent 869b1ec
Shaun authored May 02, 2011

Showing 2 changed files with 30 additions and 7 deletions. Show diff stats Hide diff stats

  1. 25  lib/i18n.js
  2. 12  lib/jst.js
25  lib/i18n.js
... ...
@@ -1,7 +1,28 @@
1 1
 // i18n
2 2
 
3  
-// _(ctx)
  3
+exports.locale = '';
  4
+
  5
+/**
  6
+ * gettext('Hello {name}', {name: 'jst'})
  7
+ */
4 8
 exports.gettext = function(ctx, args) {
5  
-  return ctx; // TODO:
  9
+  // TODO:
  10
+
  11
+  for (var name in args) {
  12
+    var re = RegExp('\\{' + name + '\\}', 'g');
  13
+    ctx = ctx.replace(re, args[name]);
  14
+  }
  15
+  return ctx;
  16
+}
  17
+
  18
+/**
  19
+ * ngettext('There is a template', 'There are {n} templates', n)
  20
+ */
  21
+exports.ngettext = function(singular, plural, n) {
  22
+  var ctx = n === 1 ? singular : plural;
  23
+
  24
+  // TODO:
  25
+
  26
+  return ctx.replace(/\{n\}/g, n);
6 27
 }
7 28
 
12  lib/jst.js
@@ -38,6 +38,7 @@ exports.addFilters = function(newFilters) {
38 38
 // compiler
39 39
 
40 40
 const prefixes = {
  41
+        n: {s: '"', c: '', v: ''},
41 42
         s: {s: '', c: '"; ', v: '" + '},
42 43
         c: {s: ' out += "', c: ' ', v: ' out += '},
43 44
         v: {s: ' + "', c: '; ', v: ' + '},
@@ -46,25 +47,26 @@ const prefixes = {
46 47
       codere = /\{[%\{] (.+?) [%\}]\}/g;
47 48
 
48 49
 var compile = exports.compile = function(ctx) {
49  
-  var m, i = 0, code = 'var out = "', last = 's';
  50
+  var m, i = 0, code = 'var out = ', last = 'n';
50 51
 
51 52
   _options.useIt = /{{ (e\()?it\./.test(ctx);
52 53
 
53 54
   ctx = ctx.replace(/[\t\r\n]/g, '').replace(/\{#.+?#\}/g, '')
54 55
 
55 56
   if (!_options.useIt) {
56  
-    code += '"; with(it) {';
  57
+    code += '""; with(it) {';
57 58
     last = 'c';
58 59
   }
59 60
 
60 61
   while ((m = codere.exec(ctx)) !== null) {
61  
-    if (m.index > 0) {
  62
+    if (m.index > 0 && m.index > i) {
62 63
       code += prefixes[last]['s'] + ctx.substring(i, m.index).replace(/"/g, '\\"');
63 64
       last = 's';
64 65
     }
65 66
 
66 67
     if (m[0].indexOf('{%') === 0) {
67 68
       code += prefixes[last]['c'] + m[1];
  69
+      if (/\)$/.test(m[1])) code += ';';
68 70
       last = 'c';
69 71
     } else if (m[0].indexOf('{{') === 0) {
70 72
       code += prefixes[last]['v'] + filters.convert(m[1]);
@@ -87,10 +89,10 @@ var compile = exports.compile = function(ctx) {
87 89
   code += 'return out;';
88 90
   //console.log(code);
89 91
 
90  
-  var fn = new Function('it, _, filters', code);
  92
+  var fn = new Function('it, _, _n, filters', code);
91 93
 
92 94
   return function(args) {
93  
-    return fn.call(this, args, i18n.gettext, filters.filters);
  95
+    return fn.call(this, args, i18n.gettext, i18n.ngettext, filters.filters);
94 96
   }
95 97
 }
96 98
 

0 notes on commit 6e46196

Please sign in to comment.
Something went wrong with that request. Please try again.