I'm submitting a ... (check one with "x")
[x] feature request
Current behavior
https://github.com/angular/angular/issues/9104#issuecomment-244909246
I don't think it is possible, like I said before it only works with static text, it won't parse text on the js code, only templates
Expected/desired behavior
Be able to translate strings used anywhere in the code, using an API.
Reproduction of the problem
What is the expected behavior?
I'm referencing the usage of $translate.instant
to exposure real use cases:
if (data._current_results === data._total) {
content = this.$translate.instant('CLIPPINGS__LIST__SUMMARY_ALL', {'num': data._current_results});
} else {
if (undefined === data._total) {
data._total = '...';
}
content = this.$translate.instant('CLIPPINGS__LIST__SUMMARY', {
'num': data._current_results,
'total': data._total
});
}
// Put 'mentions' first
data = angular.merge({}, {
mentions: mentions
}, data);
_.each(data, (value:number, key:string):void => {
if (value) {
details += value + ' ' + this.$translate.instant('CLIPPINGS__LIST__SUMMARY_TYPE_' + key) + ', ';
}
});
if (details) {
details = '(' + _.trim(details, ', ') + ')';
}
content = content.replace(':details', details);
More examples:
- Getting image's file name from exported image of a HTML rendered report:
getExportImageName(hideExtension:boolean):string {
let fileName:string;
fileName = this.$translate.instant('D_CHART_FACET_authors__EXPORT_FILENAME', {
'profileName': this.ExportService.GetProfileName(),
'period': this.ExportService.GetPeriodString(this.SearchFilter.GetPeriodFromInterval())
});
if (!Boolean(hideExtension)) {
fileName += '.png';
}
return fileName;
}
- Sometimes you're translating, sometimes use model data (could be very verbose in a template):
private _getTitle():string {
if (this.inShareColumn) {
return this.$translate.instant('COMPARISONS__SHARE_COLUMN_share_of_voice_TITLE');
} else if (this.inTotalsColumn) {
return this.$translate.instant('COMPARISONS__TOTAL_COLUMN_share_of_voice_TITLE');
} else {
return _.get<string>(this.group, 'profileName', '');
}
}
- Using a third party chart plugin (Highcharts)
this.chart = new Highcharts.Chart(<any>{
title: {
text: this.$translate.instant('REPORTS_BLOG_MAPPING_CHART_TITLE_tone').toUpperCase(),
},
xAxis: {
title: {
text: this.$translate.instant('REPORTS_BLOG_MAPPING_CHART_TITLE_tone_xaxis')
}
},
yAxis: {
min: 0,
title: {
text: this.$translate.instant('REPORTS_BLOG_MAPPING_CHART_TITLE_tone_yaxis')
}
},
plotOptions: {
scatter: {
tooltip: {
headerFormat: '<b>{point.key}</b><br>',
pointFormat: '{point.y} ' + this.$translate.instant('REPORTS_BLOG_MAPPING_CHART_mentions')
}
}
}
});
- To setup config variables and render the text without pipes as it's not always a translated string
this.config = {
requiredList: true,
bannedList: false,
allowSpaces: false,
allowComma: false,
colorsType: false,
defaultEnterAction: 'required',
requiredTooltip: this.$translate.instant('D_CLIPPING_TAGS__REQUIRED_TOOLTIP'),
bannedTooltip: this.$translate.instant('D_CLIPPING_TAGS__BANNED_TOOLTIP')
};
SetWindowTitle(title:string) {
if (!!title) {
this.$window.document.title = this.$translate.instant(title);
}
}
dateHuman(date:Date):string {
return date.getDate() + ' ' + this.$translate.instant('GLOBAL_CALENDAR_MONTH_' + date.getMonth())
+ ' ' + date.getFullYear();
}
- Sort things based on translated values:
// Sort types
tmpTypes = _.sortBy(tmpTypes, (type:string):string => {
// 'MISC' at the end
if ('MISC' === type) {
return 'zzzzz';
}
return this.$translate.instant('FACET_phrases2__TYPE_' + type);
});
GetSortedLanguages():IFacetLangDetectedCommonServiceLanguageObject[] {
// We have to sort by translated languages!
return _.sortBy(_.map(this.facetOptions, (item:string):any => {
return {
key: item,
label: this.$translate.instant('FACET_langDetected_' + item),
cssStyle: (_.includes(['english', 'catalan', 'spanish', 'french', 'italian'], item))
? {'font-weight': 'bold'} : null,
flag: _.get(this.lutFlags, item, null)
};
}), (item):string => {
return item.label.toLowerCase();
});
}
- Export raw data to CSV or Excel with translated values:
getDataExportStacked(inputData:any):any {
let exportData = angular.copy(inputData);
if (angular.isArray(exportData) && exportData.length) {
exportData[0].name = this.$translate.instant('CLIPPINGS__CHARTS_volume_TITLE');
exportData[0].data = _.map(exportData[0].data, (inputDataItem:any):any => {
return {
'label': inputDataItem.association.profileName,
'value': inputDataItem.value
};
});
}
return exportData;
}
- Set config strings to third party plugins:
UpdateCalendarStrings():void {
Highcharts.setOptions({
lang: {
months: [
this.$translate.instant('GLOBAL_CALENDAR_MONTH_January'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_February'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_March'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_April'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_May'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_June'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_July'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_August'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_September'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_October'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_November'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_December')
],
shortMonths: [
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Jan'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Feb'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Mar'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Apr'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_May'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Jun'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Jul'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Aug'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Sep'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Oct'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Nov'),
this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Dec')
],
weekdays: [
this.$translate.instant('GLOBAL_CALENDAR_DAY_Sunday'),
this.$translate.instant('GLOBAL_CALENDAR_DAY_Monday'),
this.$translate.instant('GLOBAL_CALENDAR_DAY_Tuesday'),
this.$translate.instant('GLOBAL_CALENDAR_DAY_Wednesday'),
this.$translate.instant('GLOBAL_CALENDAR_DAY_Thursday'),
this.$translate.instant('GLOBAL_CALENDAR_DAY_Friday'),
this.$translate.instant('GLOBAL_CALENDAR_DAY_Saturday')
]
}
});
}
**What is the motivation / use case for changing the behavior?**
Be able to translate strings outside templates.
Please tell us about your environment:
- Angular version: 2.0.0-rc.6
- Browser: [all]
- Language: [TypeScript 2.0.2 | ES5 | SystemJS]
@vicb
feature area: i18n freq3: high