Exploring the AngularJS framework as a beginner, I am currently working on creating a service called String
that merges the results of two resource requests. The first request is to string/:stringId.json
, and the second is to
string-'+language+'/:stringId.json
.
Everything works smoothly when the string-'+language+'/:stringId.json exists. However, if it doesn't exist, an expected 404 error is displayed in the console, but the return value of String.get(...)
ends up being empty.
Service.js
angular.module(...)
.value('Language', 'fr')
.factory('String', ['$resource', 'Language',
function($resource, language){
return jQuery.extend(
$resource('string/:stringId.json'),
$resource('string-'+language+'/:stringId.json')
);
}])
Controllers.js
angular.module('exBusiApp.controllers', [])
.controller('LayoutCtrl', ['$scope', 'String',
function($scope, String, version) {
$scope.string = String.get({stringId: "layout"});
}])
I envision calling a service in my controller that retrieves necessary strings based on the provided stringId parameter in the current language without the controller worrying about the language specifics. This leads me to the concept of merging the results of two JSON files (one for default strings and another for specific languages where defaults are used if translations are missing). Hence, the idea to merge the two resources.
When both files exist, like for example having English {hello : "hello, mistake : "mistake"} and French {hello : "bonjour"}, the view displays "bonjour" and "mistake" (as "mistake" is not included in the French file).
If anyone has any insights or suggestions, they would be greatly appreciated.