var app = angular.module('myApp', ['ngResource']);
app.controller('myController',['$scope', 'translationService',
function ($scope, translationService){
//Run translation if selected language changes
$scope.translate = function(){
translationService.getTranslation($scope, $scope.selectedLanguage);
};
//Init
$scope.selectedLanguage = 'en';
$scope.translate();
}]);
app.service('translationService', function($resource) {
this.getTranslation = function($scope, language) {
var languageFilePath = 'translation_' + language + '.json';
console.log(languageFilePath);
$resource(languageFilePath).get(function (data) {
$scope.translation = data;
});
};
});
<!DOCTYPE html>
<html>
<head>
<link data-require="bootstrap-css@3.2.0" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
<script data-require="angular-resource@1.2.14" data-semver="1.2.14" src="http://code.angularjs.org/1.2.14/angular-resource.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<section class="margin-bottom-20">
<h4>Select language for translation:</h4>
<select class="form-control" ng-change="translate()" ng-model="selectedLanguage">
<option value="en">English</option>
<option value="no">Norsk</option>
</select>
</section>
<h4>Selected langua
<strong>{{selectedLanguage}}</strong>
</h4>
<h4>Translated word goes here :</h4>
<p>
{{translation.HELLO_WORLD}} <br>
{{translation.COLOR}}<br>
{{translation.NAME}}
</p>
</div>
</body>
</html>
.form-control{
width: 40%;
}
.margin-bottom-20{
margin-bottom : 20px;
}
body{
padding-left: 10%;
padding-top: 5%;
}
{
"COLOR" : "Color",
"HELLO" : "Hello",
"HELLO_WORLD" : " Hello World!",
"NAME" :"My name is Thuy"
}
{
"COLOR" : "Farge",
"HELLO" : "Hallo",
"HELLO_WORLD" : "Hallo verden!",
"NAME" : "Mitt navn er Thuy"
}