Skip to content

Instantly share code, notes, and snippets.

@expalmer
Created March 4, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save expalmer/2bf04d9a0ef2883fee8d to your computer and use it in GitHub Desktop.
Save expalmer/2bf04d9a0ef2883fee8d to your computer and use it in GitHub Desktop.
I18n for my react progress form
;(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 );
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