Skip to content

Commit

Permalink
remove clientSideStrings part, stringNotFound added to locales/en/app…
Browse files Browse the repository at this point in the history
….json, should be added to other languages, readme updated, $rootscope.translations removed
  • Loading branch information
paglias committed Nov 14, 2013
1 parent c75bfaa commit 0c40d44
Show file tree
Hide file tree
Showing 22 changed files with 41 additions and 90 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
===============
Expand Down
4 changes: 1 addition & 3 deletions locales/bg/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/cs/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/cu/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/de/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
5 changes: 2 additions & 3 deletions locales/en/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/es/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/fr/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/he/app.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{

"languageName": "Hebrew",
"clientSideStrings": {
"removeAds": "Remove Ads"
},


"_commentfrontpage":"HABITRPG FRONT PAGE",
"synopsis" : "תוכנית שמהפך את ההיים שלך למשחק תפקידים. ההצלחה מעלה את המדרגה, הכשלון מונע את נקודות החיים, תרוויחו כסף לאסף נשקת שריון ופריטים.",
Expand Down
4 changes: 1 addition & 3 deletions locales/id/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/it/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/ja/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/nl/app.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 1 addition & 3 deletions locales/no/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/pl/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/pt/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/ru/app.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{

"languageName": "Russian",
"clientSideStrings": {
"removeAds": "Remove Ads"
},


"_commentfrontpage":"HABITRPG FRONT PAGE",
"synopsis" : "Программа, которая вырабатывает привычки, превращая вашу жизнь в ролевую игру. Получайте уровни за достижения, теряйте очки здоровья за неслеланные дела, зарабатывайте золото и покупайте оружие и броню.",
Expand Down
4 changes: 1 addition & 3 deletions locales/uk/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 1 addition & 3 deletions locales/ur/app.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
4 changes: 4 additions & 0 deletions public/js/app.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
16 changes: 8 additions & 8 deletions public/js/directives/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
];

Expand Down
30 changes: 8 additions & 22 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,43 +83,29 @@ 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);

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){
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 0c40d44

Please sign in to comment.