diff --git a/README.md b/README.md index e27762f7e2..b0d0781472 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,13 @@ ## 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 or not in english? Italian or Italiano?) and might have a `stringNotFound` property. - 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 - If the string is in a `.jade` file it can be accessed with `env.t('string name here')` -- Strings under `clientSideStrings` can be accessed in `.jade` files with their property name (ie. `env.t('myString')` not `env.t('clientSideStrings.myString')`) -- If the string is in a `.js` file it can be accessed with `window.translations['string name here']` or `$rootScope.translations` (keep both or only window.env.translations?) and the string must be placed under the `clientSideStrings` part of app.json - -## TODO -- add a "String not found." string for every language and also for client side strings? +- If the string is in a `.js` file it can be accessed with `window.env.t['string name here']` HabitRPG =============== diff --git a/locales/bg/app.json b/locales/bg/app.json index 94f5344aee..d4a0b0aafb 100644 --- a/locales/bg/app.json +++ b/locales/bg/app.json @@ -1,8 +1,6 @@ { "languageName": "Bulgarian", - "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/locales/cs/app.json b/locales/cs/app.json index d159a3b9ed..b29382a176 100644 --- a/locales/cs/app.json +++ b/locales/cs/app.json @@ -1,8 +1,6 @@ { "languageName": "Czech", - "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/locales/cu/app.json b/locales/cu/app.json index 16022e7dfd..37b003b75c 100644 --- a/locales/cu/app.json +++ b/locales/cu/app.json @@ -1,9 +1,7 @@ { "languageName": "Slavonic", - "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/locales/de/app.json b/locales/de/app.json index 089259b29e..90d862c503 100644 --- a/locales/de/app.json +++ b/locales/de/app.json @@ -1,8 +1,6 @@ { "languageName": "German", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"HABITRPG FRONT PAGE", "synopsis" : "Ein Rollenspiel für das eigene Leben - HabitRPG hilft dabei, neue Gewohnheiten zu entwickeln. Bei Erfolg winkt dir der Stufenaufstieg, Mißerfolg kostet dich Lebenspunkte, und mit dem erspielten Geld kannst du Waffen und Rüstungen kaufen.", diff --git a/locales/en/app.json b/locales/en/app.json index 05f56beed6..cd12c0bf54 100644 --- a/locales/en/app.json +++ b/locales/en/app.json @@ -1,9 +1,8 @@ { "languageName": "English", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "stringNotFound": "String not found.", + "_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/locales/es/app.json b/locales/es/app.json index e782fe7da9..fdddf5d73b 100644 --- a/locales/es/app.json +++ b/locales/es/app.json @@ -1,8 +1,6 @@ { "languageName": "Espanol", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"HABITRPG PORTADA", "synopsis" : "Un programa para construir hábitos que convierte tu vida en un Juego de Rol. Aumenta de nivel a medida que triunfas, pierde HP al fracasar, gana dinero para comprar armas y armaduras.", diff --git a/locales/fr/app.json b/locales/fr/app.json index dcf39d2854..ce61a15538 100644 --- a/locales/fr/app.json +++ b/locales/fr/app.json @@ -1,9 +1,7 @@ { "languageName": "French", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"PAGE D'ACCUEIL DE HABITRPG", "synopsis" : "Ce programme vous permet de gérer votre vie quotidienne à la manière d'un jeu de rôle (RPG). Gagnez des niveaux lorsque vous réussissez vos objectifs, perdez des points de vie lorsque vous échouez et gagnez de l'argent pour acheter des armes et des armures.", diff --git a/locales/he/app.json b/locales/he/app.json index 7333847397..db0f32e1d2 100644 --- a/locales/he/app.json +++ b/locales/he/app.json @@ -1,9 +1,7 @@ { "languageName": "Hebrew", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"HABITRPG FRONT PAGE", "synopsis" : "תוכנית שמהפך את ההיים שלך למשחק תפקידים. ההצלחה מעלה את המדרגה, הכשלון מונע את נקודות החיים, תרוויחו כסף לאסף נשקת שריון ופריטים.", diff --git a/locales/id/app.json b/locales/id/app.json index b84cb95739..6e96308bb3 100644 --- a/locales/id/app.json +++ b/locales/id/app.json @@ -1,9 +1,7 @@ { "languageName": "Indonesian", - "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/locales/it/app.json b/locales/it/app.json index 43b6ce3e02..d7b97197a0 100644 --- a/locales/it/app.json +++ b/locales/it/app.json @@ -1,9 +1,7 @@ { "languageName": "Italian", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"HABITRPG FRONT PAGE", "synopsis" : "Un programma per trattare le proprie abitudini come in un Gioco di Ruolo. Sali di livello se ci riesci, perdi HP se fallisci, guadagna soldi per comprare armi ed armature.", diff --git a/locales/ja/app.json b/locales/ja/app.json index 3e52890d01..f6100a8cdd 100644 --- a/locales/ja/app.json +++ b/locales/ja/app.json @@ -1,9 +1,7 @@ { "languageName": "Japanese", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"ハビットRPG フロントページ", "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/locales/nl/app.json b/locales/nl/app.json index b4ed00cf1e..812296ef2a 100644 --- a/locales/nl/app.json +++ b/locales/nl/app.json @@ -1,9 +1,7 @@ { "languageName": "Dutch", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"HABITRPG VOORPAGINA", "synopsis" : "Een programma om gewoontes mee op te bouwen dat je leven behandelt als een Role Playing Game. Stijg in level als je het goed doet, verlies HP als je het slecht doet, verdien geld om wapens en armor te kopen", diff --git a/locales/no/app.json b/locales/no/app.json index 2ad1f6191f..8e837fec62 100644 --- a/locales/no/app.json +++ b/locales/no/app.json @@ -1,9 +1,7 @@ { "languageName": "Norwegian", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"HABITRPG FRONT PAGE", "synopsis" : "Et vanebyggende program som viser livet ditt som et rollespill. Vær produktiv for å nå neste nivå, mist liv dersom du sluntrer unna, og tjen penger til å kjøpe våpen og annet utstyr.", diff --git a/locales/pl/app.json b/locales/pl/app.json index 2d2ade9811..a24f2b8c5e 100644 --- a/locales/pl/app.json +++ b/locales/pl/app.json @@ -1,9 +1,7 @@ { "languageName": "Polish", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"HABITRPG FRONT PAGE", "synopsis" : "Program wspomagający tworzenie nawyków, który traktuje Twoje życie jak grę RPG. Zdobywaj doświadczenie, gdy coś Ci się uda, trać zdrowie, gdy coś nie wyjdzie, zarabiaj złoto na zakup zbroi i broni.", diff --git a/locales/pt/app.json b/locales/pt/app.json index 7d6ad66cdc..9cfd24424f 100644 --- a/locales/pt/app.json +++ b/locales/pt/app.json @@ -1,9 +1,7 @@ { "languageName": "Portuguese", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"HABITRPG FRONT PAGE", "synopsis" : "Um programa para construção de hábitos que trata sua vida como um Role-Playing Game. Sobe de nível quando você sucede, perde pontos de vida quando você falha, ganha dinheiro para comprar armas e armaduras.", diff --git a/locales/ru/app.json b/locales/ru/app.json index 001f4c6336..7d46037ed6 100644 --- a/locales/ru/app.json +++ b/locales/ru/app.json @@ -1,9 +1,7 @@ { "languageName": "Russian", - "clientSideStrings": { - "removeAds": "Remove Ads" - }, + "_commentfrontpage":"HABITRPG FRONT PAGE", "synopsis" : "Программа, которая вырабатывает привычки, превращая вашу жизнь в ролевую игру. Получайте уровни за достижения, теряйте очки здоровья за неслеланные дела, зарабатывайте золото и покупайте оружие и броню.", diff --git a/locales/uk/app.json b/locales/uk/app.json index 7da9c8d356..1102ef57dc 100644 --- a/locales/uk/app.json +++ b/locales/uk/app.json @@ -1,9 +1,7 @@ { "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/locales/ur/app.json b/locales/ur/app.json index 86edf46211..d108b425bc 100644 --- a/locales/ur/app.json +++ b/locales/ur/app.json @@ -1,8 +1,6 @@ { "languageName": "Urdu", - "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 261931989f..27f3494ecd 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] || window.env.translations.stringNotFound); +} + 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 62c225f39f..95e136a997 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 607520599c..4849fee58a 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -83,23 +83,18 @@ 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); +var loadTranslations = function(locale){ + translations[locale] = require(path.join(__dirname, "/../locales/", locale, 'app.json')); } // First fetch english so we can merge with missing strings in other languages -setUpTranslations('en'); +loadTranslations('en'); fs.readdirSync(path.join(__dirname, "/../locales")).forEach(function(file) { if(file === 'en') return; - setUpTranslations(file); + loadTranslations(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,19 +102,10 @@ var langCodes = Object.keys(translations); var avalaibleLanguages = _.map(langCodes, function(langCode){ return { code: langCode, - name: translations[langCode].server.languageName + name: translations[langCode].languageName } }); -var getTranslatedString = function(locale, string){ - if(!locale || !string) throw new Error("Missing locale and/or string argument."); - // Should never be called - //if(!translations[locale]) throw new Error("Missing locale '" + locale + "'"); - - // TODO support nested, dot-separated, strings - return (translations[locale].server[string] || 'String not found.'); -} - var getUserLanguage = function(req, callback){ var getFromBrowser = function(){ var acceptable = _(req.acceptedLanguages).map(function(lang){ @@ -163,9 +149,9 @@ 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); + return (translations[language.code][string] || translations[language.code].stringNotFound); } }