i18nexus translate dynamic variable value

27 views Asked by At

how can I translate a dynamic value (productName) that comes from the database, for example the name of a flower

I tried to use this: {t("common:product_name", { productName: "Название товара", })}

And this enter image description here

but i received non-translate value, what i need to do for fix this?

1

There are 1 answers

0
Rawand On

Thats not how internationalization works, those dynamic values are for injecting a value into the translation string, but not translate the value. for example you can have a string like this Page {{count}} of {{total}}, so you can translate the string and keep the dynamic values, it can be used by t('pagination', {count: '2', total: '10'}), and the translation will be like this Page 2 of 10 for english, and Страница 2 из 10 for russian.

While you are building the application those translations are trated as normal JSON and your translation manager (eg: i18nexus, localize, etc...) has no bussiness with them, and the JSON values are used by an i18n framework, and the framework is not a live translator that can translate your values on fly. if your values MUST be translated and it is something necessary, I believe the best way is to have another table or collection in your database for i18n string translations, for example if you have a table for product with values like Product: id, name, quantity, price, etc..., you can make another table like Translation: id, language, i18nValue, etc... for storing all data translations in the database, and lastly a table for connecting them together like ProductTranslation: id, productId, translationId, field. that way you can fetch all translations related to that product or table that needs translation and use them in your page. and use the default one from the product itself as default language and fallback string. Alternatively you can have a translation API (eg: Google Translate, DeepL, etc...) that can be used to translate them on go which is not recomended at all and it will affect your application performance and can be expensive.