THE ISSUE AT HAND:
While testing the language selection function, I encountered an error message that reads as follows:
TypeError: $translate.use is not a function
I have come across similar queries on platforms like SO and various forums, but the solutions proposed have been ineffective in resolving my problem.
THE CODE SNIPPETS:
Configuration Section:
app.config(function($stateProvider, $urlRouterProvider, $translateProvider) {
$translateProvider.useSanitizeValueStrategy('sanitize');
$translateProvider.translations('EN', {
"MY_ACCOUNT": "My account",
"PROJECT_LIST": "Project List",
"SETTINGS": "Settings",
});
$translateProvider.translations("IT", {
"MY_ACCOUNT": "Mio account",
"PROJECT_LIST": "Lista progetti",
"SETTINGS": "Impostazioni",
});
$translateProvider.preferredLanguage(window.localStorage['language'] || 'EN');
$translateProvider.fallbackLanguage("EN");
Function Implementation:
$scope.select_language = function(language)
{
window.localStorage['language_code'] = language.code;
$translate.use(language.code);
$rootScope.app_language = language.code;
}
Testing Procedures:
describe('App tests', function() {
beforeEach(module('my_app.controllers'));
beforeEach(inject(function(_$controller_, _$rootScope_)
{
$controller = _$controller_;
$rootScope = _$rootScope_;
$scope = _$rootScope_.$new();
$translate = {};
var controller = $controller('MainCtrl', { $scope: $scope, $rootScope: $rootScope, $translate: $translate });
}));
describe('Language tests', function()
{
it('should properly load select_language function', function()
{
$scope.select_language({ name: "italiano", url: "img/italy.png", code: "IT"});
expect($rootScope.app_language).toEqual("IT");
});
});
THE MAIN QUERY:
What could be causing this error to appear?
How can I effectively test the functionality of $translate.use?