angular-gettext icon indicating copy to clipboard operation
angular-gettext copied to clipboard

How can I use it in a config phase

Open michelem09 opened this issue 9 years ago • 9 comments

If I'd like to translate string in the Angular routes file (in the specific case breadcrumb labels), how can I do? Gettext gets the strings but does not set them, I should use gettextcatalog.getString but if I try to add it I get an error, I'd like to have something like this:

// Setting up route
angular.module('reports').config(['$stateProvider', 'gettext', 
    function($stateProvider, gettext) {
        $stateProvider
        .state('root.reports', {
            url: '/reports',            
            views: {
                'content@': {
                    templateUrl: 'modules/reports/views/reports.main.client.view.html',
                }
            },
            data: {
                ncyBreadcrumbLabel: gettext('Reports')
            }
        })
    }
]);

Thanks for your time

michelem09 avatar Mar 24 '15 15:03 michelem09

gettextcatalog.getString is not available in the config step. Probably because translation can be load with an asynchronious http request.

I don't think angular-gettext is appropriate to translate urls. Define route is a process extremely sensitive who must not depend on an external file. Often we use to accept 2 urls : The original one (canonical version) and the translated one.

Basically you can do :

var locale = 'fr_FR';
var translatedUrl = {
    'en_US': {
        '/i/am/an/untranslated/url': "/i/am/a/translated/url"
    },
    'en_Uk': {
        '/i/am/an/untranslated/url': "/i/am/a/British/translated/url"
    },
    'fr_FR': {
        '/i/am/an/untranslated/url': "/je/suis/une/url/traduite"
    }
};

qpRouteProvider.when(translatedUrl[locale]['/i/am/an/untranslated/url'], {
    templateUrl: '/my_template.html'
});

or write you own function to do :

var locale = 'fr_FR';
var translation_table = {
    'fr_FR': {
        'user': 'utilisateur',
        'country': 'pays'
    }
};

function translateUrl(url) {
        // please do you own soup here
}

qpRouteProvider.when(translateUrl('/i/am/an/untranslated/url'), {
    templateUrl: '/my_template.html'
});

or you can write a provider to extend the function when of ngRoute to translate and register successively the canonical url and the translated url.

Good luck

GabrielDelepine avatar Mar 26 '15 09:03 GabrielDelepine

+1

@Yappli He doesn't want to translate URL but to use angular-gettext to translate the label for ncyBreadcrumbs.

knuhol avatar Apr 15 '15 17:04 knuhol

Correct.

michelem09 avatar Apr 16 '15 06:04 michelem09

@michelem09 I have opened this topic on SO.

knuhol avatar Apr 16 '15 08:04 knuhol

Hey thanks! I solved with this https://github.com/ncuillery/angular-breadcrumb/issues/65 Giving a templateUrl with the "translate" filter included.

michelem09 avatar Apr 16 '15 10:04 michelem09

This can be the solution, however the grunt tool for an extraction doesn't support it - your solution has the same issue:

app.config(function($stateProvider) {
  $stateProvider.state('welcome', {
     url : '/',
     templateUrl: 'index.html',
     ncyBreadcrumb : {
        label: '{{"Home"|translate}}'
     }
  });
});

Only possibility is to define the same string again in another place in your code.

knuhol avatar Apr 24 '15 11:04 knuhol

Hi there,

I was in the very same case as you did @Akarienta , I found this on Stack Overflow and it worked like a charm:

Both grunt tasks to extract and compile translations are working just fine with this. For information I use poedit to manage my translations.

http://stackoverflow.com/questions/30292000/gettext-module-in-angularjs-does-does-not-translate-gettextcatalog-getstring-i

Basically (quoted from Stack Overflow):

Anything that goes on the scope shouldn't use gettextCatalog.getString.

Use something like this:

$rootScope.stepText = gettext("My step 1 title"); And in the view:

{{stepText | translate}}

Cheers,

GitYouss avatar Aug 24 '15 17:08 GitYouss

'{{"Home"|translate}}' works file but what if I have to translate strings that have to be interpolated. Something like

'{{ "{{viewValue}} is less than the allowed minimum of {{schema.minimum}}" | translate }}'

can someone help please!

parvanova avatar Oct 01 '15 13:10 parvanova

Is there a way to get the current language in the config phase?

jziggas avatar Feb 07 '17 22:02 jziggas