From 1b964ccc6e92cc3a6e9741cc07e28082319f1a9c Mon Sep 17 00:00:00 2001 From: Chris Jackson Date: Sat, 6 Jun 2015 12:11:03 +0100 Subject: [PATCH] Add method to load a localised bundle https://github.com/doshprompt/angular-localization/issues/49 --- src/localization.js | 20 +++++++++++++++++++- tests/unit/serviceSpec.js | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/localization.js b/src/localization.js index 2bb9089..e111235 100644 --- a/src/localization.js +++ b/src/localization.js @@ -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; } @@ -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) { diff --git a/tests/unit/serviceSpec.js b/tests/unit/serviceSpec.js index 85ab953..508d3d4 100644 --- a/tests/unit/serviceSpec.js +++ b/tests/unit/serviceSpec.js @@ -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'); + })); }); }); \ No newline at end of file