cargo-i18n icon indicating copy to clipboard operation
cargo-i18n copied to clipboard

Allow passing language to each localization request

Open sorin-davidoi opened this issue 5 years ago • 4 comments

There should probably be an API that allows setting the language dynamically for each localization request (e.g. extracted from the Accept-Language header).

This would allow usages such as:

fn index_route(accept_language_header: String) -> String {
  let language: LanguageIdentifier = negociate_language(accept_language_header); // Uses https://github.com/mike-engel/accept-language-rs to extract the languages from the header and compute the intersection with the available languages (fetched from `LanguageLoader` instance?)
  fl!(LOADER, language, "greeting")
}

sorin-davidoi avatar Aug 29 '20 21:08 sorin-davidoi

That seems like a good idea, thanks for the suggestion!

kellpossible avatar Aug 30 '20 16:08 kellpossible

I'm wondering whether this should be added to the fl!() macro, only added to the FluentLanguageLoader and called separately? It may be difficult for the macro to distinguish between the language and other arguments if it is optional, because it lacks access to type information. At the moment, only a set of languages in a sequence of fallbacks are loaded. Some more thought about how this would impact the design and functionality is certainly required in a server context where the language may be switching for each request, and asynchronously. I'll have a bit more of a think about this tomorrow.

kellpossible avatar Aug 30 '20 17:08 kellpossible

This idea doesn't fit with the current model of LanguageLoader, having a "current language" because it could change with every request and you would want all language resources to be loaded and ready to go at a moment's notice. I'm thinking the best way to approach this might be to make a new Fluent loader type, and a new macro (that shares code with the existing one).

kellpossible avatar Sep 01 '20 07:09 kellpossible

I think the WIP pull request #62 is supposed to address this

kellpossible avatar Apr 25 '21 06:04 kellpossible