Skip to content

Commit

Permalink
modify language resources format
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Aug 1, 2016
1 parent 614aaee commit 47674cb
Show file tree
Hide file tree
Showing 6 changed files with 583 additions and 510 deletions.
1 change: 0 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ gulp.task('html', ['styles', 'scripts', 'views'], function () {

gulp.task('langs', function () {
return gulp.src('src/langs/**/*')
.pipe($.jsonminify())
.pipe(gulp.dest('dist/langs'));
});

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"gulp-eslint": "^2.1.0",
"gulp-htmlmin": "^2.0.0",
"gulp-if": "^2.0.1",
"gulp-jsonminify": "^1.0.0",
"gulp-load-plugins": "^1.2.4",
"gulp-plumber": "^1.1.0",
"gulp-replace": "^0.5.4",
Expand Down
505 changes: 0 additions & 505 deletions src/langs/zh_CN.json

This file was deleted.

502 changes: 502 additions & 0 deletions src/langs/zh_CN.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/scripts/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
optionStorageKey: 'Options',
languageStorageKeyPrefix: 'Language',
languagePath: '/langs',
languageFileExtension: '.json',
languageFileExtension: '.txt',
defaultLanguage: 'en',
defaultHost: 'localhost',
globalStatStorageCapacity: 120,
Expand Down
82 changes: 80 additions & 2 deletions src/scripts/services/ariaNgLanguageLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,83 @@
'use strict';

angular.module('ariaNg').factory('ariaNgLanguageLoader', ['$http', '$q', 'localStorageService', 'ariaNgConstants', 'ariaNgLanguages', function ($http, $q, localStorageService, ariaNgConstants, ariaNgLanguages) {
var getKeyValuePair = function (line) {
var equalSignPos = line.indexOf('=');

if (equalSignPos > 0) {
return {
key: line.substring(0, equalSignPos),
value: line.substring(equalSignPos + 1, line.length)
}
} else {
return {
value: line
};
}
};

var getCategory = function (langObj, category) {
var currentCategory = langObj;

if (!category) {
return currentCategory;
}

if (category[0] === '[' && category[category.length - 1] === ']') {
category = category.substring(1, category.length - 1);
}

if (category === 'default') {
return currentCategory;
}

var categoryNames = category.split('.');

for (var i = 0; i < categoryNames.length; i++) {
var categoryName = categoryNames[i];

if (!currentCategory[categoryName]) {
currentCategory[categoryName] = {};
}

currentCategory = currentCategory[categoryName];
}

return currentCategory;
};

var getLanguageObject = function (languageContent) {
var langObj = {};

if (!languageContent) {
return langObj;
}

var lines = languageContent.split('\n');
var currentCatagory = langObj;

for (var i = 0; i < lines.length; i++) {
var line = lines[i];

if (!line) {
continue;
}

if (/^\[.+\]$/.test(line)) {
currentCatagory = getCategory(langObj, line);
continue;
}

var pair = getKeyValuePair(line);

if (pair && pair.key) {
currentCatagory[pair.key] = pair.value;
}
}

return langObj;
};

return function (options) {
var deferred = $q.defer();

Expand All @@ -23,8 +100,9 @@
url: languagePath,
method: 'GET'
}).success(function (data) {
localStorageService.set(languageKey, data);
return deferred.resolve(data);
var languageObject = getLanguageObject(data);
localStorageService.set(languageKey, languageObject);
return deferred.resolve(languageObject);
}).error(function (data) {
return deferred.reject(options.key);
});
Expand Down

0 comments on commit 47674cb

Please sign in to comment.