Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular translate static-loader-file PascalPrecht

Tags:

angularjs

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.

like image 417
Rober Avatar asked Oct 14 '13 10:10

Rober


2 Answers

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.

like image 118
Pascal Precht Avatar answered Sep 21 '22 15:09

Pascal Precht


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.

like image 29
Rober Avatar answered Sep 23 '22 15:09

Rober