nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.74k stars 483 forks source link

How do I use string formatting with i18n-module? #1228

Closed vedmant closed 3 years ago

vedmant commented 3 years ago

Here is documentation about this for Vue I18n https://kazupon.github.io/vue-i18n/guide/formatting.html , but I see nothing about formatting in https://i18n.nuxtjs.org/ , also just trying something like this didn't work {{ $t('{team.name} now has {kills} kills on {site}.', {...item, site: $metaInfo.title}) }}. Is there no support for string formatting?

rchl commented 3 years ago

It's supported through vue-i18n but you seem to be using it in a wrong way. The $t takes the translation key and not the translation value itself.

vedmant commented 3 years ago

@rchl Thanks, can't key be a default value? It's very common way to just use english variant as a key and for translation have only translated languages values. For example it's a convention in Wordpress https://codex.wordpress.org/I18n_for_WordPress_Developers

rchl commented 3 years ago

That doesn't sound very optimal as then you'd need to update both the key and the translation when default translation changes.

Also it's less optional in terms of code size.

But in theory it's possible. You haven't shown your strings map though so can't say if it's correct.

vedmant commented 3 years ago

@rchl I guessed that it should use key as a default value, when there is no translation in translation file, for example using this approach I don't need to add default english translation at all. It looks like $t() showing key when there is no match in translations file, but not using it as a template to replace variables.

That doesn't sound very optimal as then you'd need to update both the key and the translation when default translation changes.

It depends, there are two main approaches, custom short keys, or just base language (english) strings as keys. It's common practice to use strings as keys (https://laravel.com/docs/8.x/localization#using-translation-strings-as-keys, https://codex.wordpress.org/I18n_for_WordPress_Developers). If it's a lot of translations it's becoming hard to manage all the keys manually, also to match them to translations. If most of the strings are short it's quite optimal to do things this way. Also it's easy to find and replace keys in translations files when needed. In case when translations are done in the backend database and loaded async, then it's even simpler to generate translation files this way, original english string is just updated once in DB, all translations refer it by id.

rchl commented 3 years ago

As far as I know vue-i18n doesn't support that mode of operation (where key is not defined in the translation files and key itself is used as a string). You could try making a feature request at its repo.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.