Skip to content

Commit

Permalink
add i18n support to demo client
Browse files Browse the repository at this point in the history
  • Loading branch information
Liming Xie committed Jan 12, 2015
1 parent 9f3708d commit 5d6bfaa
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 22 deletions.
Binary file modified dump.rdb
Binary file not shown.
5 changes: 5 additions & 0 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
<link rel="stylesheet" href="main.css"/>
<script src="/socket.io/socket.io.js"></script>
<script src="js/jquery-1.11.2.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/hotjs-i18n.js"></script>
<script src="js/en.lang.js"></script>
<script src="js/zh.lang.js"></script>
<script src="main.js"></script>
</head>
<body>
<div id='env'>
<span class='I18N' i18n='language'>Language</span>: <select id='lang'>
<option value='en'>English</option>
<option value='zh'>中文</option>
</select>
<h3 id="roomname"></h3>
<div id='roomdesc'></div>
<h3 id='sharedcards'></h3>
Expand Down
1 change: 1 addition & 0 deletions www/js/en.lang.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
hotjs.i18n.put('en', {
'language': 'Language',
'fastsignup': 'Fast Sign Up',
'signup': 'Sign Up',
'login': 'Login',
Expand Down
5 changes: 2 additions & 3 deletions www/js/hotjs-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ hotjs.i18n = {
setLang : function(lang, cook) {
this.lang = lang;
if (cook) {
var wl = window.location, now = new Date(), time = now.getTime();
time += this.cookievalid;
now.setTime(time);
var wl = window.location, now = new Date();
now.setTime( now.getTime() + this.cookievalid );
document.cookie = 'lang=' + lang + ';path=' + wl.pathname + ';domain=' + wl.host + ';expires=' + now.toGMTString();
}
return this;
Expand Down
117 changes: 117 additions & 0 deletions www/js/jquery.cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*!
* jQuery Cookie Plugin v1.4.1
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2006, 2014 Klaus Hartl
* Released under the MIT license
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// CommonJS
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {

var pluses = /\+/g;

function encode(s) {
return config.raw ? s : encodeURIComponent(s);
}

function decode(s) {
return config.raw ? s : decodeURIComponent(s);
}

function stringifyCookieValue(value) {
return encode(config.json ? JSON.stringify(value) : String(value));
}

function parseCookieValue(s) {
if (s.indexOf('"') === 0) {
// This is a quoted cookie as according to RFC2068, unescape...
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
}

try {
// Replace server-side written pluses with spaces.
// If we can't decode the cookie, ignore it, it's unusable.
// If we can't parse the cookie, ignore it, it's unusable.
s = decodeURIComponent(s.replace(pluses, ' '));
return config.json ? JSON.parse(s) : s;
} catch(e) {}
}

function read(s, converter) {
var value = config.raw ? s : parseCookieValue(s);
return $.isFunction(converter) ? converter(value) : value;
}

var config = $.cookie = function (key, value, options) {

// Write

if (arguments.length > 1 && !$.isFunction(value)) {
options = $.extend({}, config.defaults, options);

if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setTime(+t + days * 864e+5);
}

return (document.cookie = [
encode(key), '=', stringifyCookieValue(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}

// Read

var result = key ? undefined : {};

// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all. Also prevents odd result when
// calling $.cookie().
var cookies = document.cookie ? document.cookie.split('; ') : [];

for (var i = 0, l = cookies.length; i < l; i++) {
var parts = cookies[i].split('=');
var name = decode(parts.shift());
var cookie = parts.join('=');

if (key && key === name) {
// If second argument (value) is a function it's a converter...
result = read(cookie, value);
break;
}

// Prevent storing a cookie that we couldn't decode.
if (!key && (cookie = read(cookie)) !== undefined) {
result[name] = cookie;
}
}

return result;
};

config.defaults = {};

$.removeCookie = function (key, options) {
if ($.cookie(key) === undefined) {
return false;
}

// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
return !$.cookie(key);
};

}));
1 change: 1 addition & 0 deletions www/js/zh.lang.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
hotjs.i18n.put('zh', {
'language': '语言',
'fastsignup': '快速注册',
'signup': '注册',
'login': '登录',
Expand Down
32 changes: 24 additions & 8 deletions www/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ var Client = require('../lib/client'),

var client = null;

//hotjs.i18n.setLang('zh');

Poker.toHTML = function(cards) {
var html = '';
for(var i=0; i<cards.length; i++) {
Expand All @@ -28,6 +26,23 @@ $(document).ready(function(){

client = new Client(socket);

var lang = $.cookie('lang');
if(lang) {
$("select#lang option").filter(function() {
return $(this).val() == lang;
}).prop('selected', true);

hotjs.i18n.setLang( lang );
hotjs.i18n.translate();
}

$('select#lang').change(function(){
$( "select#lang option:selected" ).each(function() {
$.cookie('lang', $(this).val());
location.reload();
});
});

socket.on('hello', function(data){
$('#messages').empty();
$('div#cmds').empty();
Expand All @@ -41,7 +56,8 @@ $(document).ready(function(){
if(u && p) {
login(u, p);
} else {
socket.emit('hello', {});
//socket.emit('hello', {});
client.rpc('fastsignup', 0, parseSignUpReply);
}
}, 1000);
});
Expand Down Expand Up @@ -149,12 +165,12 @@ $(document).ready(function(){
});

client.on('fold', function(ret){
addMsg( ret.uid + _T('at seat') + ret.seat + _T('fold'));
addMsg( ret.uid + _T_('at seat') + ret.seat + _T_('fold'));
});

client.on('call', function(ret){
var seat = parseInt(ret.seat);
addMsg( ret.uid + _T('at seat') + seat + _T('call') + ret.call);
addMsg( ret.uid + _T_('at seat') + seat + _T_('call') + ret.call);

client.room.pot += ret.call;

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

client.room.pot += raise_sum;

Expand Down Expand Up @@ -204,15 +220,15 @@ $(document).ready(function(){

client.on('seecard', function(ret){
var seat = parseInt(ret.seat);
addMsg( ret.uid + _T('at seat') + seat + _T('seecard') );
addMsg( ret.uid + _T_('at seat') + seat + _T_('seecard') );
if(ret.cards) {
client.room.cards[ seat ] = ret.cards;
showRoom(client.room);
}
});

client.on('showcard', function(ret){
addMsg( ret.uid + _T('at seat') + ret.seat + _T('showcard') );
addMsg( ret.uid + _T_('at seat') + ret.seat + _T_('showcard') );
if(ret.cards) {
client.room.cards[ parseInt(ret.seat) ] = ret.cards;
showRoom(client.room);
Expand Down
5 changes: 5 additions & 0 deletions wwwsrc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
<link rel="stylesheet" href="main.css"/>
<script src="/socket.io/socket.io.js"></script>
<script src="js/jquery-1.11.2.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/hotjs-i18n.js"></script>
<script src="js/en.lang.js"></script>
<script src="js/zh.lang.js"></script>
<script src="main.js"></script>
</head>
<body>
<div id='env'>
<span class='I18N' i18n='language'>Language</span>: <select id='lang'>
<option value='en'>English</option>
<option value='zh'>中文</option>
</select>
<h3 id="roomname"></h3>
<div id='roomdesc'></div>
<h3 id='sharedcards'></h3>
Expand Down
1 change: 1 addition & 0 deletions wwwsrc/js/en.lang.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
hotjs.i18n.put('en', {
'language': 'Language',
'fastsignup': 'Fast Sign Up',
'signup': 'Sign Up',
'login': 'Login',
Expand Down
5 changes: 2 additions & 3 deletions wwwsrc/js/hotjs-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ hotjs.i18n = {
setLang : function(lang, cook) {
this.lang = lang;
if (cook) {
var wl = window.location, now = new Date(), time = now.getTime();
time += this.cookievalid;
now.setTime(time);
var wl = window.location, now = new Date();
now.setTime( now.getTime() + this.cookievalid );
document.cookie = 'lang=' + lang + ';path=' + wl.pathname + ';domain=' + wl.host + ';expires=' + now.toGMTString();
}
return this;
Expand Down
Loading

0 comments on commit 5d6bfaa

Please sign in to comment.