Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to load a localised bundle #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,23 @@ angular.module('ngLocalize', ['ngSanitize', 'ngLocalize.Config', 'ngLocalize.Eve
}
}

/**
* Returns the localized bundle given the bundle path.
* Note that this will not load the bundle if it's not already loaded.
* Therefore you should wrap this inside a call to ready
* @param path the name of the bundle to retrieve
* @returns {*} the bundle
*/
function getLocalizedBundle(path) {
var key = path + ".-";
var bundle = getBundle(key);
if (bundle && !bundle._loading) {
return bundle;
} else {
return null;
}
}

function getLocale() {
return currentLocale;
}
Expand All @@ -271,7 +288,8 @@ angular.module('ngLocalize', ['ngSanitize', 'ngLocalize.Config', 'ngLocalize.Eve
getKey: getKey,
setLocale: setLocale,
getLocale: getLocale,
getString: getLocalizedString
getString: getLocalizedString,
getBundle: getLocalizedBundle
};
})
.filter('i18n', function (locale) {
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/serviceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,24 @@ describe('service', function () {
it('should validate tokens with whitespace', inject(function (locale) {
expect(locale.isToken('test.hello world')).toBe(true);
}));

it('should return a bundle object', inject(function (locale) {
inject(function ($injector) {
// Set up the mock http service responses
var _httpBackend = $injector.get('$httpBackend');
// backend definition common for all tests
_httpBackend.whenGET('languages/en-US/common.lang.json').respond({
helloWorld: 'Hello World'
});

// force our service to pull down the required resource file
$injector.get('locale').ready('common');
_httpBackend.flush();
});

locale.ready('common');
var o = locale.getBundle('common');
expect(o.helloWorld).toEqual('Hello World');
}));
});
});