Title: i18n: Able to use translation strings outside a template · Issue #11405 · angular/angular · GitHub
Open Graph Title: i18n: Able to use translation strings outside a template · Issue #11405 · angular/angular
X Title: i18n: Able to use translation strings outside a template · Issue #11405 · angular/angular
Description: I'm submitting a ... (check one with "x") [x] feature request Current behavior #9104 (comment) 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/d...
Open Graph Description: I'm submitting a ... (check one with "x") [x] feature request Current behavior #9104 (comment) I don't think it is possible, like I said before it only works with static text, it won't parse text o...
X Description: I'm submitting a ... (check one with "x") [x] feature request Current behavior #9104 (comment) I don't think it is possible, like I said before it only works with static text, it ...
Opengraph URL: https://github.com/angular/angular/issues/11405
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"i18n: Able to use translation strings outside a template","articleBody":"**I'm submitting a ...** (check one with \"x\")\n\n```\n[x] feature request\n```\n\n**Current behavior**\nhttps://github.com/angular/angular/issues/9104#issuecomment-244909246\n\n> 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\n\n**Expected/desired behavior**\nBe able to translate strings used anywhere in the code, using an API.\n## **Reproduction of the problem**\n\n**What is the expected behavior?**\nI'm referencing the usage of `$translate.instant` to exposure real use cases:\n- Custom rendered text:\n\n``` ts\nif (data._current_results === data._total) {\n content = this.$translate.instant('CLIPPINGS__LIST__SUMMARY_ALL', {'num': data._current_results});\n } else {\n if (undefined === data._total) {\n data._total = '...';\n }\n\n content = this.$translate.instant('CLIPPINGS__LIST__SUMMARY', {\n 'num': data._current_results,\n 'total': data._total\n });\n }\n\n // Put 'mentions' first\n data = angular.merge({}, {\n mentions: mentions\n }, data);\n\n _.each(data, (value:number, key:string):void => {\n if (value) {\n details += value + ' ' + this.$translate.instant('CLIPPINGS__LIST__SUMMARY_TYPE_' + key) + ', ';\n }\n });\n\n if (details) {\n details = '(' + _.trim(details, ', ') + ')';\n }\n\n content = content.replace(':details', details);\n```\n\nMore examples:\n\n\u003cdetails>\n- Getting image's file name from exported image of a HTML rendered report:\n\n``` ts\ngetExportImageName(hideExtension:boolean):string {\n let fileName:string;\n\n fileName = this.$translate.instant('D_CHART_FACET_authors__EXPORT_FILENAME', {\n 'profileName': this.ExportService.GetProfileName(),\n 'period': this.ExportService.GetPeriodString(this.SearchFilter.GetPeriodFromInterval())\n });\n\n if (!Boolean(hideExtension)) {\n fileName += '.png';\n }\n\n return fileName;\n }\n```\n- Sometimes you're translating, sometimes use model data (could be very verbose in a template):\n\n``` ts\nprivate _getTitle():string {\n if (this.inShareColumn) {\n return this.$translate.instant('COMPARISONS__SHARE_COLUMN_share_of_voice_TITLE');\n } else if (this.inTotalsColumn) {\n return this.$translate.instant('COMPARISONS__TOTAL_COLUMN_share_of_voice_TITLE');\n } else {\n return _.get\u003cstring>(this.group, 'profileName', '');\n }\n }\n```\n- Using a third party chart plugin (Highcharts)\n\n``` ts\nthis.chart = new Highcharts.Chart(\u003cany>{\n title: {\n text: this.$translate.instant('REPORTS_BLOG_MAPPING_CHART_TITLE_tone').toUpperCase(),\n },\n xAxis: {\n title: {\n text: this.$translate.instant('REPORTS_BLOG_MAPPING_CHART_TITLE_tone_xaxis')\n }\n },\n yAxis: {\n min: 0,\n title: {\n text: this.$translate.instant('REPORTS_BLOG_MAPPING_CHART_TITLE_tone_yaxis')\n }\n },\n plotOptions: {\n scatter: {\n tooltip: {\n headerFormat: '\u003cb>{point.key}\u003c/b>\u003cbr>',\n pointFormat: '{point.y} ' + this.$translate.instant('REPORTS_BLOG_MAPPING_CHART_mentions')\n }\n }\n }\n });\n```\n- To setup config variables and render the text without pipes as it's not always a translated string\n\n``` ts\nthis.config = {\n requiredList: true,\n bannedList: false,\n allowSpaces: false,\n allowComma: false,\n colorsType: false,\n defaultEnterAction: 'required',\n requiredTooltip: this.$translate.instant('D_CLIPPING_TAGS__REQUIRED_TOOLTIP'),\n bannedTooltip: this.$translate.instant('D_CLIPPING_TAGS__BANNED_TOOLTIP')\n };\n```\n- To set window.title :) :\n\n``` ts\nSetWindowTitle(title:string) {\n if (!!title) {\n this.$window.document.title = this.$translate.instant(title);\n }\n }\n```\n- Custom date formatting:\n\n``` ts\ndateHuman(date:Date):string {\n return date.getDate() + ' ' + this.$translate.instant('GLOBAL_CALENDAR_MONTH_' + date.getMonth())\n + ' ' + date.getFullYear();\n }\n```\n- Sort things based on translated values:\n\n``` ts\n// Sort types\n tmpTypes = _.sortBy(tmpTypes, (type:string):string => {\n // 'MISC' at the end\n if ('MISC' === type) {\n return 'zzzzz';\n }\n\n return this.$translate.instant('FACET_phrases2__TYPE_' + type);\n });\n```\n\n``` ts\nGetSortedLanguages():IFacetLangDetectedCommonServiceLanguageObject[] {\n // We have to sort by translated languages!\n return _.sortBy(_.map(this.facetOptions, (item:string):any => {\n return {\n key: item,\n label: this.$translate.instant('FACET_langDetected_' + item),\n cssStyle: (_.includes(['english', 'catalan', 'spanish', 'french', 'italian'], item))\n ? {'font-weight': 'bold'} : null,\n flag: _.get(this.lutFlags, item, null)\n };\n }), (item):string => {\n return item.label.toLowerCase();\n });\n }\n```\n- Export raw data to CSV or Excel with translated values:\n\n``` ts\ngetDataExportStacked(inputData:any):any {\n let exportData = angular.copy(inputData);\n\n if (angular.isArray(exportData) && exportData.length) {\n exportData[0].name = this.$translate.instant('CLIPPINGS__CHARTS_volume_TITLE');\n\n exportData[0].data = _.map(exportData[0].data, (inputDataItem:any):any => {\n return {\n 'label': inputDataItem.association.profileName,\n 'value': inputDataItem.value\n };\n });\n }\n\n return exportData;\n }\n```\n- Set config strings to third party plugins:\n\n``` ts\nUpdateCalendarStrings():void {\n Highcharts.setOptions({\n lang: {\n months: [\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_January'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_February'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_March'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_April'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_May'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_June'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_July'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_August'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_September'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_October'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_November'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_December')\n ],\n shortMonths: [\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Jan'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Feb'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Mar'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Apr'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_May'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Jun'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Jul'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Aug'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Sep'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Oct'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Nov'),\n this.$translate.instant('GLOBAL_CALENDAR_MONTH_SHORT_Dec')\n ],\n weekdays: [\n this.$translate.instant('GLOBAL_CALENDAR_DAY_Sunday'),\n this.$translate.instant('GLOBAL_CALENDAR_DAY_Monday'),\n this.$translate.instant('GLOBAL_CALENDAR_DAY_Tuesday'),\n this.$translate.instant('GLOBAL_CALENDAR_DAY_Wednesday'),\n this.$translate.instant('GLOBAL_CALENDAR_DAY_Thursday'),\n this.$translate.instant('GLOBAL_CALENDAR_DAY_Friday'),\n this.$translate.instant('GLOBAL_CALENDAR_DAY_Saturday')\n ]\n }\n });\n }\n```\n\n\u003c/details>\n**What is the motivation / use case for changing the behavior?**\nBe able to translate strings outside templates.\n\n**Please tell us about your environment:**\n- **Angular version:** 2.0.0-rc.6\n- **Browser:** [all] \n- **Language:** [TypeScript 2.0.2 | ES5 | SystemJS]\n\n@vicb \n","author":{"url":"https://github.com/marcalj","@type":"Person","name":"marcalj"},"datePublished":"2016-09-07T08:13:57Z","url":"https://github.com/angular/angular/issues/11405"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d99ef53f-c342-d4c1-c18f-13f6bcb605f6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D53A:31E99C:1CA4A10:262F4E3:6B0165AA |
| html-safe-nonce | 35f115d825659b144145b00de9db410c4ce731222fe594d54bbad0e9133a2cee |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTNBOjMxRTk5QzoxQ0E0QTEwOjI2MkY0RTM6NkIwMTY1QUEiLCJ2aXNpdG9yX2lkIjoiMjQ1MjYwMTg1MDIzOTczOTMwNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0934fa81f5219a02aac09bcc800a41961fda155009735189a006a03808b59fd7 |
| hovercard-subject-tag | issue:175439759 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/11405/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cbd85f613e0bdd0542b87c7c876f72a534fe92525a7ce72f1ef9a72bf775e9a2/angular/angular/issues/11405 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cbd85f613e0bdd0542b87c7c876f72a534fe92525a7ce72f1ef9a72bf775e9a2/angular/angular/issues/11405 |
| og:image:alt | I'm submitting a ... (check one with "x") [x] feature request Current behavior #9104 (comment) I don't think it is possible, like I said before it only works with static text, it won't parse text o... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | marcalj |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width