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.

Translation is not applied in simple TS

See original GitHub issue

We are using ttag in normal JSX and it works perfectly.

However, we’re also loading some data from TS files and ttag does not work properly there.

Working example:

// Taste.tsx
<h2 className="title center-text">{t`Taste`}</h2>

Non-working example:

// (same file)
const getTastes = () => {
    return tastes.map((taste, index) => {
      return (
        <li className="tastes" key={index}>
          <Circle size="2em" color={taste.color} />
          <p className="option">{taste.name}</p>
        </li>
      );
    });
  };

<ul>{getTastes()}</ul>

// (Taste.ts)

export const TASTE = new Map<string, ITaste>([
  ['FLORAL', { name: t`Floral`, color: '#F3B8B7' }],

‘Floral’ is still showing up as the given text.

Some things seem to be working:

  • Floral is extracted to the .po file
  • Floral is in the JSON file with the translation
  • In the output, there is no trace of the t

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AlexMostcommented, May 23, 2019

Hi @lroellin! I suppose the problem here is that this code is executed before ttag locale was applied:

export const TASTE = new Map<string, ITaste>([
  ['FLORAL', { name: t`Floral`, color: '#F3B8B7' }],

You should ensure that you call useLocale before any ttag functions are executed. You can look here for example: https://github.com/ttag-org/CRA-runtime-example/blob/master/app/src/App.js#L1

Here we are setting up the appropriate language in the first import of the app entry point:

import './i18nInit'

Please, let me know if that works for you.

Alternatively, you can also use function to execute t later:

export const TASTE = () => 

//...
0reactions
lroellincommented, Jun 6, 2019

Hi! We now fixed the issue by always setting the locale (we have a side-effecty import). Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngx translate - Using get or instant method in ts file is not ...
in app.module.ts try: import {APP_INITIALIZER, Injector, NgModule} from '@angular/core'; import {LOCATION_INITIALIZED} from ...
Read more >
How to translate your Angular app with ngx-translate
1. Add ngx-translate to your Angular application 2. Set up the TranslateService in your app.module.ts 3. Create your main language translation file (in...
Read more >
Internationalization and Localization with Qt Quick | Qt 6.4
ts files are XML files with the source texts and a place for the translated text. The updated .ts files are converted into...
Read more >
Making Your App Ready for Translations
The basic concept is that all strings which must be translated are separated into a JSON file per language. The app's html and...
Read more >
Angular I18n: How to Internationalize with ngx-translate | Phrase
The ngx-translate library allows us to change the language of the application at runtime without reloading the whole app. However, Angular ...
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