I´m using Angular-translate (PascalPrecht module).
I successed to make it work according to the example in: http://www.ng-newsletter.com/posts/angular-translate.html
However, I would like to have all my translations in different json files using loader-static-files, but this is not working for me. I´m not sure I´m doing right here.
In my angular module I just replaced the code working (now commented) with the loader-static call:
angular.module('myApp.i18n', ['pascalprecht.translate'])
.config(['$translateProvider', function ($translateProvider) {
/*$translateProvider.translations('en', {
HOME: 'Home',
COMPANIES: 'Companies',
WHAT_TO_DO: 'What to do',
ABOUT: 'About us',
CONTACT: 'Contact'
})
.translations('es', {
HOME: 'Inicio',
COMPANIES: 'Empresas',
WHAT_TO_DO: 'Qué hacer',
ABOUT: 'Nosotros',
CONTACT: 'Contacto'
});*/
$translateProvider.preferredLanguage('es');
$translateProvider.useStaticFilesLoader({
prefix: '/languages/',
suffix: '.json'
});
}]);
I have added to my app folder the files:
/app/languages/en_US.json /app/languages/es.json
When I load my home site, I see next error in console:
http://localhost:1234/languages/es.json 404 (Not Found)
If I remove the first '/' in my prefix, like this:
$translateProvider.useStaticFilesLoader({
prefix: 'languages/',
suffix: '.json'
});
Then, I get next error in console:
Unexpected token H
Sorry, I think I don´t undertand how this should work.
Make sure that your server sends the .json
as json and not as plain text.
Also, make sure that the names for the .json files depend on the corresponding language key.
As you can read here: http://angular-translate.github.io/docs/#/guide/12_asynchronous-loading ("Using staticFilesLoader")
The code looks good to me, so once you've fixed the issue that your server isn't sending json as json, things should work.
I fixed it. As Pascal said in the comments, the problem was that the json file had a wrong format. You have to put all the values between double quotes.
The path was right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With