angular-gettext
angular-gettext copied to clipboard
How can I use it in a config phase
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
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
+1
@Yappli He doesn't want to translate URL but to use angular-gettext to translate the label for ncyBreadcrumbs.
Correct.
@michelem09 I have opened this topic on SO.
Hey thanks! I solved with this https://github.com/ncuillery/angular-breadcrumb/issues/65 Giving a templateUrl with the "translate" filter included.
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.
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,
'{{"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!
Is there a way to get the current language in the config phase?