question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use $localize in ts component

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/localize

Is this a regression?

I don’t know

Description

I’m using $localize method in my .ts component:

text = $localize`:@@loginError:`;

I have this key in my xlf file:

      <trans-unit id="loginError">
        <source>test stub</source>
        <target>Invalid login or password :(</target>
      </trans-unit>

But my message is not translated when i serve app

🌍 Your Environment

Angular Version:


     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 9.0.0-rc.10
Node: 13.6.0
OS: darwin x64

Angular: 9.0.0-rc.11
... animations, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.900.0-rc.10
@angular-devkit/build-angular     0.900.0-rc.10
@angular-devkit/build-optimizer   0.900.0-rc.10
@angular-devkit/build-webpack     0.900.0-rc.10
@angular-devkit/core              9.0.0-rc.10
@angular-devkit/schematics        9.0.0-rc.10
@angular/cdk                      8.2.0
@angular/cli                      9.0.0-rc.10
@angular/material                 8.2.0
@ngtools/webpack                  9.0.0-rc.10
@schematics/angular               9.0.0-rc.10
@schematics/update                0.900.0-rc.10
rxjs                              6.5.4
typescript                        3.7.5
webpack                           4.41.2

Anything else relevant? my package.json “dependencies”: { “@angular/animations”: “^9.0.0-rc.11”, “@angular/cdk”: “^8.2.0”, “@angular/common”: “~9.0.0-rc.11”, “@angular/compiler”: “~9.0.0-rc.11”, “@angular/core”: “~9.0.0-rc.11”, “@angular/forms”: “~9.0.0-rc.11”, “@angular/localize”: “9.0.0-rc.11”, “@angular/material”: “^8.2.0”, “@angular/platform-browser”: “~9.0.0-rc.11”, “@angular/platform-browser-dynamic”: “~9.0.0-rc.11”, “@angular/router”: “~9.0.0-rc.11”, “@types/rx”: “^4.1.1”, “hammerjs”: “^2.0.8”, “mobx”: “^5.11.0”, “mobx-angular”: “3.0.3”, “ngx-ace-wrapper”: “^8.0.0”, “ngx-socket-io”: “^2.1.1”, “rxjs”: “~6.5.4”, “rxjs-compat”: “^6.5.2”, “tslib”: “^1.10.0”, “zone.js”: “~0.10.2” },

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
petebacondarwincommented, Feb 12, 2020

@TimBailey-pnk - the problem is that $localize is replaced at compile-time with the translated strings. In your case you are passing it a variable that is only known at runtime. $localize is not designed for this kind of runtime behaviour. You have a couple of options:

  1. if the text that changes is part of the business logic of the application then I would argue that $localize is not the right tool for the job. As an extreme example, translate.google.com would not use $localize to display the text that is being translated for the user. In that case you own logic should replace the text at runtime using your own code. That being said you could probably load up the translation files that $localize is using and use it in your own logic to access the same translation strings if that was helpful.

  2. if the bit of the text that needs to change at runtime is not critical to selecting the translation then you could use interpolation within the translation string. I.E.

$localize `some text ${someRuntimeValue} more text`;

I appreciate that in this scenario neither of these is probably going to fit. One question is where does this data.title come from? Is it hard coded into each route’s configuration somehow? If so perhaps you could apply the $localize at that point rather than inside the component?

1reaction
petebacondarwincommented, May 6, 2020

@juanjinario - well the thing is that $localize is not yet a publicly supported API 😃 So there is no documentation on it yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular library, Cannot find name $localize - Stack Overflow
My main app have @angular/localize as dependency and @angular/localize import in polyfills.ts . Every usage of $localize in my library code ...
Read more >
$localize Global Import Migration - Angular
If you're using i18n, this schematic adds an import statement for @angular/localize to polyfills.ts that will look something like this: content_copy
Read more >
Internationalization with @angular/localize | Ninja Squad
Error: It looks like your application or one of its dependencies is using i18n. Angular 9 introduced a global `$localize()` function ...
Read more >
The Ultimate Guide to Angular Localization | Phrase
Learn end-to-end Angular localization with the built-in i18n library and ... Let's take a look at the Home component in a bit more...
Read more >
Localizing your app - Angular Hispano
Setting the "localize" option to true will cause an error when using ng serve if more than one locale is defined. Setting the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found