0

I want to use regex in i18n file, something like below:

"device /some dynamic word/ is here": "دستگاه /some dynamic word/ اینجاست"

And when using a key like below:

{{"device x33 is here"|translate}}

It should be translate to:

دستگاه x33 اینجاست

I read several approach for this issue, one of the best way is to use Args in translate pipe. But it's not possible to change any file except i18n file. Is there any way?

Mostafa Farhani
  • 305
  • 2
  • 16
  • The data come from a server and i just have a pattern. I should place the pattern in translate key till every words that match the pattern completely replace in translate value. – Mostafa Farhani Jun 29 '20 at 08:08
  • I get sentences as translate key and some words in those sentences are change. – Mostafa Farhani Jun 29 '20 at 08:26
  • I have a live implementation of your problem's solution ready but I just want to be confirmed about your requirement exactly. That's why I'm asking. So what you can do is whichever word you want to change you can add them in `ar-Ar.json` file inside i18. Isn't it? –  Jun 29 '20 at 08:29
  • I implemented the sample replica of your problem. Please see [**here**](https://ngx-translate-example-juta1j.stackblitz.io) and do let me know if you want something else. If it is fine I'll attach the code too. In this code it doesn't matter what text comes; it will translate only those texts which are mentioned inside `ar-AR.json` –  Jun 29 '20 at 08:32
  • I can't see that link! it seems there is an error – Mostafa Farhani Jun 29 '20 at 09:15
  • Is it? Wait I'll add the workspace link. Please check [**this**](https://stackblitz.com/edit/ngx-translate-example-juta1j) –  Jun 29 '20 at 09:25
  • Not that! the translation file is like this: { "HOME": { "device /regex/ is here":"دستگاه /regex/ اینجاست" } } and usage is like this: {{ 'HOME.device x33 is here' | translate }} – Mostafa Farhani Jun 29 '20 at 09:39
  • I have to translate sentences absolutely together. – Mostafa Farhani Jun 29 '20 at 09:41
  • I could thing of a logic here, convert the value from string to `Regexp` object and then parse it. Since JSON cannot use regex. You need to edit your code in order to implement your requirement. Sadly there is no other way around. –  Jun 29 '20 at 09:48
  • 1
    Thanks bro. I wish find a way – Mostafa Farhani Jun 29 '20 at 09:50
  • Please do add as answer if you find the way that works for you. –  Jun 29 '20 at 09:50

1 Answers1

0

Use can use parameters in translations of ngx-translate.

Try

{{ "device " + name + " is here" | translate:{ name: 'x33' } }}