Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to pass in translator to tr! #20

Open
bk2204 opened this issue Oct 26, 2023 · 1 comment
Open

Option to pass in translator to tr! #20

bk2204 opened this issue Oct 26, 2023 · 1 comment

Comments

@bk2204
Copy link

bk2204 commented Oct 26, 2023

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
Copy link
Member

ogoffart commented Feb 3, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants