Reputation: 740
I'm trying to create a multilanguage Angular app. I want to use angular-translate , but I don't understand how it works. I have a json which I get using $http.get , recieved data I write it to $scope and using ng-repeat and etc. output data in view. How can I output data using translate? I want to get translations from json files and use same json structure as in example if it possible. Looking for advices.
Angular app
(function(){
var app = angular.module('webApp', []);
app.controller('InfoCtrl', function($scope, $http){
$scope.services = [];
$scope.gallery = [];
$scope.clients = [];
$http.get('assets/translations/translate.json').success(function(data) {
// console.log("success!");
$scope.services = data[0].services;
$scope.gallery = data[1].gallery;
$scope.clients = data[2].clients;
});
});
})();
JSON
[
{
"services": [
{
"icon": "money",
"title": "Service 1",
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and"
},
{
"icon": "cogs",
"title": "Service 2",
"description": "Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical"
},
{
"icon": "building",
"title": "Service 3",
"description": "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at"
},
{
"icon": "industry",
"title": "Service 4",
"description": "Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of 'de Finibus Bonorum et Malorum' (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during"
}
]
},
{
"gallery": [
{
"image": "image-1.jpg"
},
{
"image": "image-2.jpg"
},
{
"image": "image-3.jpg"
},
{
"image": "image-4.jpg"
},
{
"image": "image-5.jpg"
},
{
"image": "image-6.jpg"
},
{
"image": "image-7.jpg"
},
{
"image": "image-8.jpg"
}
]
},
{
"clients": [
{
"name": "Aaron",
"title": "Designer",
"image": "face-aaroni.jpg",
"logo": "logo1.png",
"review": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia nesciunt harum natus ea praesentium corporis, eius nam nihil aliquam necessitatibus incidunt ullam quis at blanditiis, voluptates, doloribus! Atque libero expedita aut natus molestiae ratione culpa neque ut voluptatum beatae nulla, repellat praesentium iusto vel ipsa assumenda, optio eum, totam, quia?"
},
{
"name": "Atari",
"title": "Programmer",
"image": "face-atariboy.jpg",
"logo": "logo2.png",
"review": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia nesciunt harum natus ea praesentium corporis, eius nam nihil aliquam necessitatibus incidunt ullam quis at blanditiis, voluptates, doloribus! Atque libero expedita aut natus molestiae ratione culpa neque ut voluptatum beatae nulla, repellat praesentium iusto vel ipsa assumenda, optio eum, totam, quia?"
},
{
"name": "Janna",
"title": "QA Ingerneer",
"image": "face-jackiesaik.jpg",
"logo": "logo3.png",
"review": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia nesciunt harum natus ea praesentium corporis, eius nam nihil aliquam necessitatibus incidunt ullam quis at blanditiis, voluptates, doloribus! Atque libero expedita aut natus molestiae ratione culpa neque ut voluptatum beatae nulla, repellat praesentium iusto vel ipsa assumenda, optio eum, totam, quia?"
}
]
}
]
Example views for different $scope
arrays
<div class="service-wrapper">
<div class="service" ng-repeat='service in services'>
<h2><i class="fa fa-{{service.icon}}"></i>{{service.title}}</h2>
<p>{{service.description}}</p>
</div>
</div>
<div class="gallery-wrapper">
<a class="gallery-img" href="assets/images/gallery/{{image.image}}" data-lightbox="work" ng-repeat='image in gallery'><img ng-src="assets/images/gallery/{{image.image}}" alt="Image"></a>
</div>
<div class="client-unit" ng-class="{'active-client': $first}" ng-repeat='client in clients'>
<figure class="client-face">
<img ng-src="assets/images/clients/{{client.image}}" alt="client-face">
<figcaption>
<strong class="client-name">{{client.name}}</strong>
<em class="client-title">{{client.title}}</em>
</figcaption>
</figure>
<div class="client-content">
<p>{{client.review}}</p>
</div>
</div>
Upvotes: 4
Views: 153
Reputation: 28269
Angular-translate can help you internationalizing client-side strings.
In short, you have to feed the $translationProvider with a json containing your translations:
$translateProvider.translations('en', {
'home.title': 'Welcome in my app',
'button.cancel': 'Cancel'
});
and then use the translate filter:
<h1>{{'home.title' | translate}}</h1>
<h1>{{::'home.title' | translate}}</h1> <!-- one-time binding with angular 1.3+ -->
or the directive
<h1 translate="home.title"></h1>
<h1 translate>home.title</h1>
In terms of performance prefer one of the last three syntaxes. The first one, {{'home.title' | translate}}
, keeps watchers in memory.
Refer to the documentation for the other features of angular-translate. Here is the getting-started guide.
Translation of the strings of the JSON you posted
Seeing the json you posted makes me think that you want to translate some service-specific strings. Their translation should be done in the back-end, i.e. the server should return the translated text according to the chosen language.
Upvotes: 1