Instantly share code, notes, and snippets.
Created
November 14, 2013 09:40
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save lefnire/7464100 to your computer and use it in GitHub Desktop.
shared server/client app.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/README.md b/README.md | |
index b466355..41f8af7 100644 | |
--- a/README.md | |
+++ b/README.md | |
@@ -1,15 +1,7 @@ | |
# Translations - WIP | |
-Some files has been overwriteen after merging with develop, will revisit later: | |
- | |
-- locales/nl/app.json | |
-- locales/bg/app.json | |
- | |
------------------ | |
- | |
## How to add a new language | |
-- All the old files in `/locales` has been moved to `/locales_old`. | |
-- New files must have a `languageName` property (translated or not in english? Italian or Italiano?) and strings that have to be accessed in a `.js` file must be placed inside the `clientSideStrings` of `app.json`. | |
+- New files must have a `languageName` property (translated, eg Italiano) and strings that have to be accessed in a `.js` file must be placed inside the `clientSideStrings` of `app.json`. | |
- The language folder under `/locales` must be named with a two letters code (en, es... not en_US or similar) and the code must match the one passed by the browser (we should find a table for that...) | |
## How to translate a string | |
diff --git a/locales/en/app.json b/locales/en/app.json | |
index 05f56be..7eda936 100644 | |
--- a/locales/en/app.json | |
+++ b/locales/en/app.json | |
@@ -1,9 +1,6 @@ | |
{ | |
"languageName": "English", | |
- "clientSideStrings": { | |
- "removeAds": "Remove Ads" | |
- }, | |
"_commentfrontpage":"HABITRPG FRONT PAGE", | |
"synopsis" : "A habit building program which treats your life like a Role Playing Game. Level up as you succeed, lose HP as you fail, earn money to buy weapons and armor.", | |
diff --git a/public/js/app.js b/public/js/app.js | |
index 2619319..f729d0e 100644 | |
--- a/public/js/app.js | |
+++ b/public/js/app.js | |
@@ -1,5 +1,9 @@ | |
"use strict"; | |
+window.env.t = function(string){ | |
+ return (window.env.translations[string] || 'String not found.'); | |
+} | |
+ | |
window.habitrpg = angular.module('habitrpg', | |
['ngResource', 'ngSanitize', 'userServices', 'groupServices', 'memberServices', 'challengeServices', | |
'sharedServices', 'authServices', 'notificationServices', 'guideServices', | |
diff --git a/public/js/directives/directives.js b/public/js/directives/directives.js | |
index 62c225f..95e136a 100644 | |
--- a/public/js/directives/directives.js | |
+++ b/public/js/directives/directives.js | |
@@ -131,21 +131,21 @@ habitrpg | |
scope.main = attrs.main; | |
$rootScope.lists = [ | |
{ | |
- header: 'Habits', | |
+ header: env.t('Habits'), | |
type: 'habit', | |
- placeHolder: 'New Habit' | |
+ placeHolder: env.t('newHabit') | |
}, { | |
- header: 'Dailies', | |
+ header: env.t('Dailies'), | |
type: 'daily', | |
- placeHolder: 'New Daily' | |
+ placeHolder: env.t('newDaily') | |
}, { | |
- header: 'To-Dos', | |
+ header: env.t('Todos'), | |
type: 'todo', | |
- placeHolder: 'New To-Do' | |
+ placeHolder: env.t('newTodo') | |
}, { | |
- header: 'Rewards', | |
+ header: env.t('Rewards'), | |
type: 'reward', | |
- placeHolder: 'New Reward' | |
+ placeHolder: env.t('newReward') | |
} | |
]; | |
diff --git a/src/middleware.js b/src/middleware.js | |
index 6075205..563d665 100644 | |
--- a/src/middleware.js | |
+++ b/src/middleware.js | |
@@ -84,11 +84,7 @@ var getManifestFiles = function(page){ | |
var translations = {}; | |
var setUpTranslations = function(locale){ | |
- var t = translations[locale] = {}; | |
- t.server = require(path.join(__dirname, "/../locales/", locale, 'app.json')); | |
- t.client = t.server.clientSideStrings; | |
- delete t.server.clientSideStrings; | |
- _.merge(t.server, t.client); | |
+ translations[locale] = require(path.join(__dirname, "/../locales/", locale, 'app.json')); | |
} | |
// First fetch english so we can merge with missing strings in other languages | |
@@ -98,8 +94,7 @@ fs.readdirSync(path.join(__dirname, "/../locales")).forEach(function(file) { | |
if(file === 'en') return; | |
setUpTranslations(file); | |
// Merge missing strings from english | |
- _.defaults(translations[file].server, translations.en.server); | |
- _.defaults(translations[file].client, translations.en.client); | |
+ _.defaults(translations[file], translations.en); | |
}); | |
var langCodes = Object.keys(translations); | |
@@ -107,7 +102,7 @@ var langCodes = Object.keys(translations); | |
var avalaibleLanguages = _.map(langCodes, function(langCode){ | |
return { | |
code: langCode, | |
- name: translations[langCode].server.languageName | |
+ name: translations[langCode].languageName | |
} | |
}); | |
@@ -117,7 +112,7 @@ var getTranslatedString = function(locale, string){ | |
//if(!translations[locale]) throw new Error("Missing locale '" + locale + "'"); | |
// TODO support nested, dot-separated, strings | |
- return (translations[locale].server[string] || 'String not found.'); | |
+ return (translations[locale][string] || 'String not found.'); | |
} | |
var getUserLanguage = function(req, callback){ | |
@@ -163,7 +158,7 @@ module.exports.locals = function(req, res, next) { | |
getBuildUrl: getBuildUrl, | |
avalaibleLanguages: avalaibleLanguages, | |
language: language, | |
- translations: translations[language.code].client, | |
+ translations: translations[language.code], | |
t: function(string){ | |
return getTranslatedString(language.code, string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment