Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit

  • Loading branch information...
commit 8b8dd2976cc14c5249dfdde34158ee8df3d0b5b1 0 parents
@baryshev authored
3  .npmignore
@@ -0,0 +1,3 @@
+*.md
+.git*
+examples/
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2012 Vadim M. Baryshev
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 README.md
@@ -0,0 +1,10 @@
+# About
+Locale loader for multilingual applications.
+
+# Installation
+
+ npm install localejs
+
+# Example
+
+See [examples](https://github.com/baryshev/localejs/tree/master/examples) folder.
24 examples/example.js
@@ -0,0 +1,24 @@
+var
+ locale = require('./../index');
+
+/* Init */
+
+locale.configure({ root : __dirname + '/locale' });
+
+/* /Init */
+
+
+/* Usage */
+
+var localeRu = locale.get('ru');
+var localeEn = locale.get('en');
+
+console.log(localeRu.data.common.helloWorld);
+console.log(localeRu.data.blog.addPost);
+console.log(localeRu.plural(localeRu.data.blog.comments, 42));
+
+console.log(localeEn.data.common.helloWorld);
+console.log(localeEn.data.blog.addPost);
+console.log(localeEn.plural(localeEn.data.blog.comments, 42));
+
+/* /Usage */
4 examples/locale/en/blog.js
@@ -0,0 +1,4 @@
+module.exports = {
+ addPost: 'Add post',
+ comments: [ '%d comment', '%d comments' ]
+};
3  examples/locale/en/common.js
@@ -0,0 +1,3 @@
+module.exports = {
+ helloWorld: 'Hello, World!'
+};
4 examples/locale/en/index.js
@@ -0,0 +1,4 @@
+module.exports = {
+ common: require('./common'),
+ blog: require('./blog')
+};
4 examples/locale/ru/blog.js
@@ -0,0 +1,4 @@
+module.exports = {
+ addPost: 'Добавить запись',
+ comments: [ '%d комментарий', '%d комментария', '%d комментариев' ]
+};
3  examples/locale/ru/common.js
@@ -0,0 +1,3 @@
+module.exports = {
+ helloWorld: 'Здравствуй, Мир!'
+};
4 examples/locale/ru/index.js
@@ -0,0 +1,4 @@
+module.exports = {
+ common: require('./common'),
+ blog: require('./blog')
+};
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/localejs');
40 lib/localejs.js
@@ -0,0 +1,40 @@
+var
+ path = require('path'),
+ sprintf = require('sprintf').sprintf;
+
+var
+ options = {
+ root : ''
+ };
+
+module.exports.configure = function(newOptions) {
+ for (var option in options) {
+ options[option] = newOptions[option] || options[option];
+ }
+};
+
+module.exports.get = function(locale) {
+ var localeHolder = {};
+ localeHolder.data = require(path.normalize(options.root + '/' + locale + '/index'));
+ localeHolder.plural = function(string, number) {
+ switch (locale) {
+ case 'ru' :
+ var absNumber = Math.abs(number) % 100;
+ var n1 = absNumber % 10;
+ if (absNumber > 10 && absNumber < 20) return sprintf(string[2], number);
+ if (n1 > 1 && n1 < 5) return sprintf(string[1], number);
+ if (n1 == 1) return sprintf(string[0], number);
+ return sprintf(string[2], number);
+ break;
+ case 'en' :
+ if (Math.abs(number) == 1) return sprintf(string[0], number);
+ return sprintf(string[1], number);
+ break;
+ default :
+ if (Math.abs(number) == 1) return sprintf(string[0], number);
+ return sprintf(string[1], number);
+ break;
+ }
+ };
+ return localeHolder;
+};
13 package.json
@@ -0,0 +1,13 @@
+{
+ "name" : "localejs",
+ "version" : "0.1.0",
+ "description" : "Locale loader",
+ "keywords": ["locale"],
+ "author" : "Vadim M. Baryshev <vadimbaryshev@gmail.com>",
+ "repository" : { "type" : "git", "url" : "https://github.com/baryshev/localejs.git" },
+ "main" : "index",
+ "dependencies": {
+ "sprintf": ">= 0.1.1"
+ },
+ "engines" : { "node": ">= 0.4.0" }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.