Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
feat(service): add $translate.getTranslationTable(langKey)
Browse files Browse the repository at this point in the history
This adds the service method `$translate.getTranslationTable(langKey)`. It will return the *current* translation table of the given/current language.

=> It can return null if the langKey is invalid or the translations are still in progress.
=> It can return incomplete results if a partial loader is used.
=> It can return null if the no current language is selected (whenever).
=> The table cannot be modified used for changing the service’s internal data (it’s a copy).

This commit based on work by Ovchar Stanislav <stanislav.ovchar@motorolasolutions.com> / Stas Ovchar <head.crab.meat@gmail.com>

Solves #1578
Solves #1374
  • Loading branch information
knalli committed Oct 30, 2016
1 parent d92bca0 commit 40f9e35
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,30 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
return null;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translate#getTranslationTable
* @methodOf pascalprecht.translate.$translate
*
* @description
* Returns translation table by the given language key.
*
* Unless a language is provided it returns a translation table of the current one.
* Note: If translation dictionary is currently downloading or in progress
* it will return null.
*
* @param {string} langKey A token which represents a translation id
*
* @return {object} a copy of angular-translate $translationTable
*/
$translate.getTranslationTable = function (langKey) {
langKey = langKey || $translate.use();
if (langKey && $translationTable[langKey]) {
return angular.copy($translationTable[langKey]);
}
return null;
};

// Whenever $translateReady is being fired, this will ensure the state of $isReady
var globalOnReadyListener = $rootScope.$on('$translateReady', function () {
$onReadyDeferred.resolve();
Expand Down
41 changes: 41 additions & 0 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ describe('pascalprecht.translate', function () {
expect($translate.isForceAsyncReloadEnabled).toBeDefined();
});

it('should have a method getTranslationTable()', function () {
expect($translate.getTranslationTable).toBeDefined();
});

it('should not try to load a language which has not been registered yet', function () {
// ensure initial language is en (preferred one)
expect($translate.use()).toBe('en');
Expand Down Expand Up @@ -2775,6 +2779,43 @@ describe('pascalprecht.translate', function () {

});

describe('$translate#getTranslationTable()', function () {
var ru_RU_translationMock = {'HI': 'Всем привет!'};
var en_EN_translationMock = {'HI': 'Hello there!'};

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider
.translations('ru_RU', ru_RU_translationMock)
.translations('en_EN', en_EN_translationMock)
.preferredLanguage('ru_RU');
}));

var $translate;

beforeEach(inject(function (_$translate_) {
$translate = _$translate_;
}));

it('should return translation table which is curently in use if no params', function () {
expect(angular.equals($translate.getTranslationTable(), ru_RU_translationMock)).toBe(true);
});

it('should return translation table by translation Id', function () {
expect(angular.equals($translate.getTranslationTable('en_EN'), en_EN_translationMock)).toBe(true);
});

it('should return a copy of translation table', function () {
var copy = $translate.getTranslationTable('en_EN');
copy.HI = "what's up";
expect(angular.equals($translate.getTranslationTable('en_EN'), en_EN_translationMock)).toBe(true);
});

it('should return null if translation table with translation Id does not exist', function () {
expect($translate.getAvailableLanguageKeys('NOT_EXISTING_TRANSLATION_ID')).toEqual(null);
});

});

describe('$translate#postprocess() with an enabled fallback language', function () {

describe('single post processDemo', function () {
Expand Down

0 comments on commit 40f9e35

Please sign in to comment.