0

Introduction

Hello, I have an Angular+Ionic project and I'm using Transloco to translate some of the text.

The problem

I have a consts file that has strings inside and I want to be able to translate them in the same file but I don't know how to do this since I can't import the TranslocoService on a consts file.

The code

test.consts.ts

 export const LABELS = {
      hello_world: 'Hello World!',
      goodbye: "Goodbye"
    }

es.json

{
    "label_1": "Hola mundo!",
    "label_2": "Adios"
}

Alternatives

I thought about an alternatives in case it's not possible to translate on the consts file:

  1. Using the translation key instead of the phrase: For example replacing Hello World! with label_1. Then when using the consts file I would have to translate the value, for example: this.translocoSrv.t(LABELS.label_1)

While this alternative works it would take a lot of time to implement. I would have to replace every consts file and refactor every component, template, etc. that uses the consts files.

Thanks in advance.

Matias
  • 1
  • 1

1 Answers1

0

If I understood correctly, your issue is with the type "const". Without ever having worked with transloco, I would generally recomend you check out their sample solutions on stackblitz for example.

Check out this project: https://stackblitz.com/edit/ngneat-transloco?file=src%2Fassets%2Fi18n%2Fes.json

I dont find them using any "const" files, they seem to just store their data in .json files.

Here you can see they store their translations as json in the assets folder enter image description here

  • Hello, no. The consts and translations files are 2 different type of files. The json files are for the translations and the consts files are files used in an Angular project to store constants. – Matias Jul 05 '21 at 15:31
  • I added a better example now to make it easier to understand my question. – Matias Jul 05 '21 at 15:36