woboq / tr

Translation tools for rust
57 stars 9 forks source link

Option to pass in translator to `tr!` #20

Open bk2204 opened 11 months ago

bk2204 commented 11 months ago

I like this crate and I'd like to use it to translate some of my own crates. However, some of them can be used as libraries and I don't want to hold global state per-module.

Instead, I'd prefer to pass in some translator in my code (probably in the logging structure I have) to let users choose this value themselves. They might do that using the environment, or they might decide that they prefer to acquire it from an HTTP request (i.e., Accept-Language), or some other way; I don't care very much, but I want it to not be a process global.

Is it possible that tr! could be expanded to do that, or that a similar macro could be added with that functionality? If you're generally positive on the idea, I'm happy to try my hand at such a change.

ogoffart commented 8 months ago

I think it's a valid request. I wonder what syntax to put.
tr!(my_translator, "....") seems like an obvious choice, but i'm afraid $tanslator:expr would be ambiguous with the string literal. also might be harder for xtr to parse.

Maybe another macro: tr2!(my_translator, "...") (but i'm not fan of the name) Or perhaps as an argument at the end: tr("...", @translator = my_translator)?

or my_translator.with(|| tr!("...")) (where my_translator actually sets the global just for the time of its inner function)