Created
March 4, 2015 13:09
-
-
Save expalmer/2bf04d9a0ef2883fee8d to your computer and use it in GitHub Desktop.
I18n for my react progress form
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
;(function ( I18n, undefined ) { | |
var defaultLocale = "en-US"; | |
I18n.locale = defaultLocale; | |
I18n.translations = []; | |
I18n.translations["en-US"] = { | |
name: { | |
label: "Your Name", | |
rules: { | |
required: 'required here', | |
regex: 'regex here' | |
} | |
}, | |
age: { | |
label: "Your Age", | |
rules: { | |
required: 'required here', | |
integer: 'integer here' | |
} | |
}, | |
email: { | |
label: "Your E-Mail", | |
rules: { | |
required: 'required here', | |
email: 'email here' | |
} | |
}, | |
lang: { | |
label: "Your Favorite Language", | |
rules: { | |
required: 'required here', | |
regex: 'regex here' | |
} | |
} | |
}; | |
I18n.translations["pt-BR"] = { | |
name: { | |
label: "Seu Nome", | |
rules: { | |
required: 'campo obrigatório', | |
regex: 'informe o nome e sobrenome' | |
} | |
}, | |
age: { | |
label: "Sua Idade", | |
rules: { | |
required: 'campo obrigatório', | |
integer: 'somente números' | |
} | |
}, | |
email: { | |
label: "Seu E-mail", | |
rules: { | |
required: 'campo obrigatório', | |
email: 'informe um e-mail válido' | |
} | |
}, | |
lang: { | |
label: "Sua linguagem favorita", | |
rules: { | |
required: 'campo obrigatório', | |
regex: 'linguagem errada: digite "javascript" ;)' | |
} | |
} | |
}; | |
I18n.getTranslation = function () { | |
return this.translations[ this.locale ] || this.translations[ defaultLocale ]; | |
}; | |
I18n.translate = function ( scope ) { | |
var translation = this.getTranslation(); | |
var scopes = scope.split("."); | |
while ( scopes.length ) { | |
translation = translation[ scopes.shift() ]; | |
if( translation === undefined || translation === null ) { | |
break; | |
} | |
} | |
return translation || 'Whoops!'; | |
}; | |
I18n.t = I18n.translate; | |
})( typeof exports === undefined ? ( this.I18n || (this.I18n = {})) : exports ); |
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
var locale = require('browser-locale'); | |
var I18n = require('../utils/I18n'); | |
I18n.locale = locale(); | |
console.log( I18n.t("name.rules.required") ); | |
console.log( I18n.t("name.label") ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment