I initially included the languages en, fr & nl in my app.js file. Realizing this was not a good practice, I moved the translations out of the app.js file and placed them in JSON formatted files.
However, upon loading the site now, I encountered a TypeError in my controller when trying to use the changeLanguage method: $translate.use is not a function. Can anyone pinpoint where I went wrong?
(BEGIN CODE SNIPPET)
app.js:
var app = angular.module('eva', ['ui.router', 'ngMaterial', 'ngMessages',
'controllers', 'factories', 'ngAnimate', '720kb.socialshare',
'angular-loading-bar', 'angular-svg-round-progress', 'pascalprecht.translate', 'ngSanitize',
'facebook']);
app.config(function ($translateProvider) {
$translateProvider.useSanitizeValueStrategy('sanitize');
$translateProvider.useStaticFilesLoader({
prefix: '../languages/',
suffix: '.json'
});
$translateProvider.registerAvailableLanguageKeys(['en', 'nl', 'fr'], {
'en': 'en',
'nl': 'nl',
'fr': 'fr'
});
$translateProvider.preferredLanguage('nl');
});
index.html:
<body layout="column" ng-controller="AppCtrl" layout-fill>
<div id="fb-root"></div>
<md-toolbar layout="row" md-scroll-shrink ng-if="true">
...
</md-toolbar>
appController:
angular.module('controllers')
.controller('AppCtrl', ['$scope', '$mdSidenav',
'$mdToast','$translate', 'auth', 'Facebook','$state', function($scope, $mdSidenav, $translate, auth, Facebook,$state){
/* toggle sidenav */
$scope.toggleSidenav = function(menuId) {
$mdSidenav(menuId).toggle();
};
/* change language */
$scope.changeLanguage = function(langKey){
$translate.use(langKey);
$state.reload();
};
The error occurs at the $translate.use(langKey); line in the code above.
You can view my JSON file structure here: https://i.sstatic.net/OsIAn.jpg.
And this is an example of one of my JSON language files: https://i.sstatic.net/H2cio.jpg.
Here's a snapshot of how the site looks: https://i.sstatic.net/jmh6d.jpg.
If there are any missing characters like ) or ; at the end of code snippets, it's because I only copied the relevant parts of the page. Rest assured they are complete.
Can anyone help identify the cause of the error?