Skip to content

Commit 5d6bfaa

Browse files
author
Liming Xie
committed
add i18n support to demo client
1 parent 9f3708d commit 5d6bfaa

13 files changed

+300
-22
lines changed

dump.rdb

-849 Bytes
Binary file not shown.

www/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
<link rel="stylesheet" href="main.css"/>
77
<script src="/socket.io/socket.io.js"></script>
88
<script src="js/jquery-1.11.2.min.js"></script>
9+
<script src="js/jquery.cookie.js"></script>
910
<script src="js/hotjs-i18n.js"></script>
1011
<script src="js/en.lang.js"></script>
1112
<script src="js/zh.lang.js"></script>
1213
<script src="main.js"></script>
1314
</head>
1415
<body>
1516
<div id='env'>
17+
<span class='I18N' i18n='language'>Language</span>: <select id='lang'>
18+
<option value='en'>English</option>
19+
<option value='zh'>中文</option>
20+
</select>
1621
<h3 id="roomname"></h3>
1722
<div id='roomdesc'></div>
1823
<h3 id='sharedcards'></h3>

www/js/en.lang.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
hotjs.i18n.put('en', {
2+
'language': 'Language',
23
'fastsignup': 'Fast Sign Up',
34
'signup': 'Sign Up',
45
'login': 'Login',

www/js/hotjs-i18n.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ hotjs.i18n = {
5959
setLang : function(lang, cook) {
6060
this.lang = lang;
6161
if (cook) {
62-
var wl = window.location, now = new Date(), time = now.getTime();
63-
time += this.cookievalid;
64-
now.setTime(time);
62+
var wl = window.location, now = new Date();
63+
now.setTime( now.getTime() + this.cookievalid );
6564
document.cookie = 'lang=' + lang + ';path=' + wl.pathname + ';domain=' + wl.host + ';expires=' + now.toGMTString();
6665
}
6766
return this;

www/js/jquery.cookie.js

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*!
2+
* jQuery Cookie Plugin v1.4.1
3+
* https://github.com/carhartl/jquery-cookie
4+
*
5+
* Copyright 2006, 2014 Klaus Hartl
6+
* Released under the MIT license
7+
*/
8+
(function (factory) {
9+
if (typeof define === 'function' && define.amd) {
10+
// AMD
11+
define(['jquery'], factory);
12+
} else if (typeof exports === 'object') {
13+
// CommonJS
14+
factory(require('jquery'));
15+
} else {
16+
// Browser globals
17+
factory(jQuery);
18+
}
19+
}(function ($) {
20+
21+
var pluses = /\+/g;
22+
23+
function encode(s) {
24+
return config.raw ? s : encodeURIComponent(s);
25+
}
26+
27+
function decode(s) {
28+
return config.raw ? s : decodeURIComponent(s);
29+
}
30+
31+
function stringifyCookieValue(value) {
32+
return encode(config.json ? JSON.stringify(value) : String(value));
33+
}
34+
35+
function parseCookieValue(s) {
36+
if (s.indexOf('"') === 0) {
37+
// This is a quoted cookie as according to RFC2068, unescape...
38+
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
39+
}
40+
41+
try {
42+
// Replace server-side written pluses with spaces.
43+
// If we can't decode the cookie, ignore it, it's unusable.
44+
// If we can't parse the cookie, ignore it, it's unusable.
45+
s = decodeURIComponent(s.replace(pluses, ' '));
46+
return config.json ? JSON.parse(s) : s;
47+
} catch(e) {}
48+
}
49+
50+
function read(s, converter) {
51+
var value = config.raw ? s : parseCookieValue(s);
52+
return $.isFunction(converter) ? converter(value) : value;
53+
}
54+
55+
var config = $.cookie = function (key, value, options) {
56+
57+
// Write
58+
59+
if (arguments.length > 1 && !$.isFunction(value)) {
60+
options = $.extend({}, config.defaults, options);
61+
62+
if (typeof options.expires === 'number') {
63+
var days = options.expires, t = options.expires = new Date();
64+
t.setTime(+t + days * 864e+5);
65+
}
66+
67+
return (document.cookie = [
68+
encode(key), '=', stringifyCookieValue(value),
69+
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
70+
options.path ? '; path=' + options.path : '',
71+
options.domain ? '; domain=' + options.domain : '',
72+
options.secure ? '; secure' : ''
73+
].join(''));
74+
}
75+
76+
// Read
77+
78+
var result = key ? undefined : {};
79+
80+
// To prevent the for loop in the first place assign an empty array
81+
// in case there are no cookies at all. Also prevents odd result when
82+
// calling $.cookie().
83+
var cookies = document.cookie ? document.cookie.split('; ') : [];
84+
85+
for (var i = 0, l = cookies.length; i < l; i++) {
86+
var parts = cookies[i].split('=');
87+
var name = decode(parts.shift());
88+
var cookie = parts.join('=');
89+
90+
if (key && key === name) {
91+
// If second argument (value) is a function it's a converter...
92+
result = read(cookie, value);
93+
break;
94+
}
95+
96+
// Prevent storing a cookie that we couldn't decode.
97+
if (!key && (cookie = read(cookie)) !== undefined) {
98+
result[name] = cookie;
99+
}
100+
}
101+
102+
return result;
103+
};
104+
105+
config.defaults = {};
106+
107+
$.removeCookie = function (key, options) {
108+
if ($.cookie(key) === undefined) {
109+
return false;
110+
}
111+
112+
// Must not alter options, thus extending a fresh object...
113+
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
114+
return !$.cookie(key);
115+
};
116+
117+
}));

www/js/zh.lang.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
hotjs.i18n.put('zh', {
2+
'language': '语言',
23
'fastsignup': '快速注册',
34
'signup': '注册',
45
'login': '登录',

www/main.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ var Client = require('../lib/client'),
77

88
var client = null;
99

10-
//hotjs.i18n.setLang('zh');
11-
1210
Poker.toHTML = function(cards) {
1311
var html = '';
1412
for(var i=0; i<cards.length; i++) {
@@ -28,6 +26,23 @@ $(document).ready(function(){
2826

2927
client = new Client(socket);
3028

29+
var lang = $.cookie('lang');
30+
if(lang) {
31+
$("select#lang option").filter(function() {
32+
return $(this).val() == lang;
33+
}).prop('selected', true);
34+
35+
hotjs.i18n.setLang( lang );
36+
hotjs.i18n.translate();
37+
}
38+
39+
$('select#lang').change(function(){
40+
$( "select#lang option:selected" ).each(function() {
41+
$.cookie('lang', $(this).val());
42+
location.reload();
43+
});
44+
});
45+
3146
socket.on('hello', function(data){
3247
$('#messages').empty();
3348
$('div#cmds').empty();
@@ -41,7 +56,8 @@ $(document).ready(function(){
4156
if(u && p) {
4257
login(u, p);
4358
} else {
44-
socket.emit('hello', {});
59+
//socket.emit('hello', {});
60+
client.rpc('fastsignup', 0, parseSignUpReply);
4561
}
4662
}, 1000);
4763
});
@@ -149,12 +165,12 @@ $(document).ready(function(){
149165
});
150166

151167
client.on('fold', function(ret){
152-
addMsg( ret.uid + _T('at seat') + ret.seat + _T('fold'));
168+
addMsg( ret.uid + _T_('at seat') + ret.seat + _T_('fold'));
153169
});
154170

155171
client.on('call', function(ret){
156172
var seat = parseInt(ret.seat);
157-
addMsg( ret.uid + _T('at seat') + seat + _T('call') + ret.call);
173+
addMsg( ret.uid + _T_('at seat') + seat + _T_('call') + ret.call);
158174

159175
client.room.pot += ret.call;
160176

@@ -174,7 +190,7 @@ $(document).ready(function(){
174190
client.on('raise', function(ret){
175191
var seat = parseInt(ret.seat);
176192
var raise_sum = (ret.call + ret.raise);
177-
addMsg( ret.uid + _T('at seat') + seat + _T('raise') + ret.raise + ' (' + raise_sum + ')');
193+
addMsg( ret.uid + _T_('at seat') + seat + _T_('raise') + ret.raise + ' (' + raise_sum + ')');
178194

179195
client.room.pot += raise_sum;
180196

@@ -204,15 +220,15 @@ $(document).ready(function(){
204220

205221
client.on('seecard', function(ret){
206222
var seat = parseInt(ret.seat);
207-
addMsg( ret.uid + _T('at seat') + seat + _T('seecard') );
223+
addMsg( ret.uid + _T_('at seat') + seat + _T_('seecard') );
208224
if(ret.cards) {
209225
client.room.cards[ seat ] = ret.cards;
210226
showRoom(client.room);
211227
}
212228
});
213229

214230
client.on('showcard', function(ret){
215-
addMsg( ret.uid + _T('at seat') + ret.seat + _T('showcard') );
231+
addMsg( ret.uid + _T_('at seat') + ret.seat + _T_('showcard') );
216232
if(ret.cards) {
217233
client.room.cards[ parseInt(ret.seat) ] = ret.cards;
218234
showRoom(client.room);

wwwsrc/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
<link rel="stylesheet" href="main.css"/>
77
<script src="/socket.io/socket.io.js"></script>
88
<script src="js/jquery-1.11.2.min.js"></script>
9+
<script src="js/jquery.cookie.js"></script>
910
<script src="js/hotjs-i18n.js"></script>
1011
<script src="js/en.lang.js"></script>
1112
<script src="js/zh.lang.js"></script>
1213
<script src="main.js"></script>
1314
</head>
1415
<body>
1516
<div id='env'>
17+
<span class='I18N' i18n='language'>Language</span>: <select id='lang'>
18+
<option value='en'>English</option>
19+
<option value='zh'>中文</option>
20+
</select>
1621
<h3 id="roomname"></h3>
1722
<div id='roomdesc'></div>
1823
<h3 id='sharedcards'></h3>

wwwsrc/js/en.lang.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
hotjs.i18n.put('en', {
2+
'language': 'Language',
23
'fastsignup': 'Fast Sign Up',
34
'signup': 'Sign Up',
45
'login': 'Login',

wwwsrc/js/hotjs-i18n.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ hotjs.i18n = {
5959
setLang : function(lang, cook) {
6060
this.lang = lang;
6161
if (cook) {
62-
var wl = window.location, now = new Date(), time = now.getTime();
63-
time += this.cookievalid;
64-
now.setTime(time);
62+
var wl = window.location, now = new Date();
63+
now.setTime( now.getTime() + this.cookievalid );
6564
document.cookie = 'lang=' + lang + ';path=' + wl.pathname + ';domain=' + wl.host + ';expires=' + now.toGMTString();
6665
}
6766
return this;

0 commit comments

Comments
 (0)