Skip to content

Commit

Permalink
Merge pull request #108 from gameclosure/feature-trans
Browse files Browse the repository at this point in the history
Feature trans
  • Loading branch information
chris-gc committed Jul 24, 2013
2 parents f6e13b9 + a024c61 commit ce91601
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/squill
18 changes: 9 additions & 9 deletions src/PackageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ var GCPackage = (function() {
return listFiles(this.paths.resources, exts, next);
};

GCPackage.prototype.listResourcesSync = function (exts, next) {
GCPackage.prototype.listResourcesSync = function (exts) {
if (typeof exts == 'function') {
cb = exts; exts = null;
}
Expand All @@ -319,23 +319,23 @@ var GCPackage = (function() {
return listFiles(this.paths.resources, ['png', 'jpg', 'jpeg', 'bmp', 'gif'], cb);
};

GCPackage.prototype.listImagesSync = function(cb) {
GCPackage.prototype.listImagesSync = function() {
return listFilesSync(this.paths.resources, ['png', 'jpg', 'jpeg', 'bmp', 'gif']);
};

GCPackage.prototype.listSounds = function(cb) {
return listFiles(this.paths.resources, ['mp3', 'ogg'], cb);
};

GCPackage.prototype.listSoundsSync = function(cb) {
return listFilesSync(this.paths.lang, ['mp3', 'ogg']);
GCPackage.prototype.listSoundsSync = function() {
return listFilesSync(this.paths.resources, ['mp3', 'ogg']);
};

GCPackage.prototype.listConfig = function(cb) {
return listFiles(this.paths.resources, ['json'], cb);
};

GCPackage.prototype.listConfigSync = function(cb) {
GCPackage.prototype.listConfigSync = function() {
return listFilesSync(this.paths.resources, ['json']);
};

Expand All @@ -345,7 +345,7 @@ var GCPackage = (function() {
return listFiles(this.paths.shared, ['js'], cb);
};

GCPackage.prototype.listScriptsSync = function(cb) {
GCPackage.prototype.listScriptsSync = function() {
return listFilesSync(this.paths.shared, ['js']);
};

Expand Down Expand Up @@ -398,7 +398,7 @@ var GCPackage = (function() {
return listFiles(this.paths.lang, ['json'], cb);
};

GCPackage.prototype.listTranslationsSync = function(cb) {
GCPackage.prototype.listTranslationsSync = function() {
return listFilesSync(this.paths.lang, ['json']);
};

Expand All @@ -412,7 +412,7 @@ var GCPackage = (function() {
}
};

GCPackage.prototype.getTranslationSync = function(code, cb) {
GCPackage.prototype.getTranslationSync = function(code) {
try {
var data = fs.readFileSync(path.join(this.paths.lang, code + ".json"));
return data ? JSON.parse(data) : null;
Expand All @@ -425,7 +425,7 @@ var GCPackage = (function() {
fs.unlink(path.join(this.paths.lang, code + ".json"), cb);
};

GCPackage.prototype.removeTranslationSync = function(code, cb) {
GCPackage.prototype.removeTranslationSync = function(code) {
fs.unlinkSync(path.join(this.paths.lang, code + ".json"));
return true
};
Expand Down
6 changes: 6 additions & 0 deletions src/serve/plugins/i18n/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": "0.1.0",
"author": "Game Closure",
"name": "Internationalization",
"scope": "i18n"
}
56 changes: 56 additions & 0 deletions src/serve/plugins/i18n/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/** @license
* This file is part of the Game Closure SDK.
*
* The Game Closure SDK is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License v. 2.0 as published by Mozilla.
* The Game Closure SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public License v. 2.0 for more details.
* You should have received a copy of the Mozilla Public License v. 2.0
* along with the Game Closure SDK. If not, see <http://mozilla.org/MPL/2.0/>.
*/

// Internationalization plugin

var fs = require('fs');
var path = require('path');
var etag = require('../../etag');
var projectManager = require('../../../ProjectManager');

var _app;

projectManager.on('AddProject', serveProject);

var _trans = {};

function serveProject (project) {
var id = project.getID();
var langPath = path.join(project.paths.root, 'resources', 'lang');

if (_trans[id] || !fs.existsSync(langPath)) { return false; }

var trans = _trans[id] = {};
var langs = fs.readdirSync(langPath);
var shouldServe = false;

for (var i = 0; i < langs.length; i++) {
var fname = langs[i];
var lparts = fname.split('.');
if (lparts[0] != 'all' && lparts[1] == 'json') {
shouldServe = true;
trans[lparts[0]] = JSON.parse(fs.readFileSync(path.join(langPath, fname)));
}
}

if (shouldServe) {
fs.writeFileSync(path.join(langPath, 'all.json'), JSON.stringify(trans));
_app.use('/simulate/' + id + '/lang/', etag.static(langPath));
}
}

exports.load = function (app) {
_app = app;
};
96 changes: 96 additions & 0 deletions src/serve/plugins/i18n/static/Pane.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/** @license
* This file is part of the Game Closure SDK.
*
* The Game Closure SDK is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License v. 2.0 as published by Mozilla.
* The Game Closure SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public License v. 2.0 for more details.
* You should have received a copy of the Mozilla Public License v. 2.0
* along with the Game Closure SDK. If not, see <http://mozilla.org/MPL/2.0/>.
*/

import sdkPlugin;
import util.ajax;
import squill.Cell;
import squill.TabbedPane;
import squill.models.DataSource as DataSource;

var transHowTo = "You haven't set up any translations yet. Add your language-specific json files to resources/lang.";

var TranslationCell = Class(squill.Cell, function() {
this._def = {
className: 'translationCell',
children: [
{
id: 'key', type: 'label', className: 'translationKey'
},
{
id: 'value', type: 'label', className: 'translationValue'
}
]
};

this.render = function() {
this.key.setLabel(this._data.key);
this.value.setLabel(this._data.value);
};
});

exports = Class(sdkPlugin.SDKPlugin, function(supr) {
this._def = {
children: [
{
className: 'topTabs',
id: 'translationTabs',
type: squill.TabbedPane,
panes: []
}
]
};

this.buildTranslations = function(err, trans) {
this.translationTabs.clear();
if (err) {
this.translationTabs.newPane({
className: 'mainPanel',
title: 'no translations yet!',
children: [{
id: 'noTranslations',
text: transHowTo,
style: { padding: '20px' }
}]
});
} else {
for (var k in trans) {
var ds = new DataSource({ key: 'key' });
for (var key in trans[k]) {
ds.add({ key: key, value: trans[k][key] });
}
this.translationTabs.newPane({
className: 'mainPanel',
title: k,
children: [{
id: k + 'List',
className: 'darkPanel translationList',
margin: 10,
type: 'list',
controller: this,
cellCtor: TranslationCell,
dataSource: ds
}]
});
}
}
};

this.onBeforeShow = function() {
util.ajax.get({
url: this._project.url + 'lang/all.json',
type: 'json'
}, bind(this, 'buildTranslations'));
};
});
30 changes: 30 additions & 0 deletions src/serve/plugins/i18n/static/i18n.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
translationListMargin = 15px

.translationList
absoluteBox(translationListMargin, translationListMargin, translationListMargin, translationListMargin)
overflow: auto;

.translationCell
color #EEE
font-size 12px
cursor pointer
border-top 1px solid rgba(0, 0, 0, 0)
border-bottom 1px solid rgba(0, 0, 0, 0)
padding 10px
font-family 'Gill Sans'

.translationCell:nth-child(even)
background-color rgba(0, 0, 0, 0.4)

.translationCell .translationKey
color #EEF
text-shadow none
width 320px
font-size 15px
margin 0
text-align left

.translationCell .translationKey
.translationCell .translationValue
display inline-block
text-overflow: ellipsis;
1 change: 1 addition & 0 deletions src/serve/stylesheets/plugins.styl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
@import "native/static/native.styl"
@import "projects/static/projects.styl"
@import "simulate/static/simulate.styl"
@import "i18n/static/i18n.styl"

0 comments on commit ce91601

Please sign in to comment.