Replies: 1 comment 1 reply
-
@MrFoxPro thanks for you kind words. async formatterMaking formatters async would result in making the whole translation process async. So instead of <h1>{LL.my.key()}</h1> you would need to write <h1>{await LL.my.key()}</h1> everywhere. That's not ideal so I'm against that change. For your example above you should probably not use a formatter, you should call this function in your template like this: import { locale } from './src/i18n/i18n-react' // import depends on the adapter used
const username = await requestRandomApi(locale)
<h1>{LL.my.key(username)}</h1> if you still want to use a formatter, you could use an approach like this: import type { FormattersInitializer } from 'typesafe-i18n'
import type { Formatters, Locales } from './i18n-types'
const randomFirstnames: string[] = []
let i = 0
// you would need to make sure you call this from outside before
// initializing `typesafe-i18n` to already have some random values in the array
export const loadNext100RandomFirstNames = async () =>
randomFirstnames = [...randomFirstnames, ...await requestRandomApi(locale)]
const getRandomFirstname = (locale: Locales) => {
i++
if (i < randomFirstnames.length - 10) {
// when you see you don't have that much values left,
// trigger the loading of new in the background
loadNext100RandomFirstNames()
}
return randomFirstnames[i]
}
export const initFormatters: FormattersInitializer<Locales, Formatters> = (locale: Locales) => {
const formatters: Formatters = {
randomFirstname: getRandomFirstname(locale)
}
return formatters
} nestingI think nesting makes sense only when you have a lot of formatters. I haven't seen more than 10 formatters in a project yet, so I would assume it is not relly needed since it makes the implementation more complicated and adds some bytes to the bundle size. automatic translationTake a look at the I would love to provide such a functionality out-of-the box. Maybe you want to help me out and write that script to include it in a PR? |
Beta Was this translation helpful? Give feedback.
-
Hello. Thank you for great tool.
I'm thinking about using a random value in the formatter, but I'm more likely to get the values from the API. Is it possible to make the formatter asynchronous then?
Also , it could be useful to allow nested formatters, like:
After switching from vscode i18-ally I would like to be able to automatically translate missing keys, for example, through a generator parameter. I tried to make it myself, but it is too complex because of need to build typescript AST. Probably I can help with it.
Beta Was this translation helpful? Give feedback.
All reactions