Using L10N in a library and a binary #5
-
Hello, thanks for the library! It's working great for my need, the compile time check makes working with localization much more pleasant. I've been using this project for a bit, and I have a binary crate that uses it. I want to use L10N for both the library, and the binary. This means that I need two different localization folder, but I haven't seen anything in the documentation about this. Is it supported? And if so, how? Minimal reproducible example: /// -- lib/l10n/en/lib.ftl --
Loc-lib = Localisation of the lib.
/// -------------------------
/// -- bin/l10n/en/bin.ftl --
Loc-bin = Localisation of the bin.
/// -------------------------
/// -- lib.rs --
l10n::init({});
message!("lib", "Loc-lib").translate(&langid!("en"));
// ------------
/// -- bin.rs --
use lib::L10N;
// Errors here, "bin" does not exist
message!("bin", "Loc-bin").translate(&langid!("en"));
// ------------ |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Sorry for the late answer, I didn't find a solution to what you're asking for. I tried using I would try in the future to add an environment var like But right now your best shot is to do something like this:
|
Beta Was this translation helpful? Give feedback.
-
Hello, thanks for the response! This is also what I though about doing at first. This works fine for a monorepo or for stuff like xtask, but for completely different crate that don't share the repo it's a bit annoying. Nonetheless this works so I'll close and mark as answered, thank you. |
Beta Was this translation helpful? Give feedback.
-
I might have misunderstood your question. I assumed you were referring to using different localization directories within a single package that contains multiple crates (one library, one or more binaries), all under one
If you have distinct packages, this is possible, but it can lead to frustration if you don't fully understand how it works under the hood, and my documentation doesn't explain this correctly... This is how
So you can see at line 4 that the path So your dependencies (library crates) should use a special config file
Then
So it will work correctly during your development flow and when running, provided that the localization directories (whether relative or not) for your binary and dependencies are present and you run the binary from the right directory (or using This means that if you compile your code on one machine and deploy it on another, you need to replicate the localization directories with the correct paths and launch your binary from the right directory. However, if you compile directly on the deployed machine, it should be fine. I'm really not happy with all of this, and I plan to embed the translations directly into the crates for the next version. This way, you won't have to worry about these issues, especially when deploying. Thank you for bringing up this issue. It allows me to explain some of the flaws of my crate. Unfortunately, I don't have time to work on it right now, but I hope to provide a new version in the coming months to address this. I have created a repository to give you an example how it works without and with a workspace: https://github.com/MathieuTricoire/l10n-example-deps |
Beta Was this translation helpful? Give feedback.
Sorry for the late answer, I didn't find a solution to what you're asking for.
I tried using
build scripts
to pass env. var but they are passed to all targets, I would be surprised there is not a way to do that with cargo but I didn't find how for now.I would try in the future to add an environment var like
L10N_PATH
that could be set like what it's done in theenv_logger
crateL10N_PATH=[target][=][path][,...]
so we could do something likeL10N_PATH=binary_target_name=l10n_bin,library_target_name=l10n_lib
.And maybe go a little bit further in situation where the binary and the library have the same name, specifying the target "type" as a prefix for disambiguation, that would give someth…