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

Simple fmt based localization using serde/miniserde #87

Open
kellpossible opened this issue Feb 20, 2022 · 3 comments
Open

Simple fmt based localization using serde/miniserde #87

kellpossible opened this issue Feb 20, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@kellpossible
Copy link
Owner

kellpossible commented Feb 20, 2022

Provide a localization method that accepts strings coming from a dictionary via a serde, nanoserde, miniserde, microserde format such as json, yaml, toml, ron, etc. Or perhaps also some binary formats like messagepack or bincode.

Strings can contain formatting syntax which can be either specified by the user, or use something like ufmt, runtime_fmt, dynfmt or something like minijinja/handlebars.

The primary use cases would be:

  • Support backwards compatibility with existing data or legacy pipelines.
  • Make it possible to have an extremely small/simple localization system for embedded/no_std use cases. This will also involve seeing how to make i18n-embed support no_std.
@kellpossible
Copy link
Owner Author

And perhaps having the full flexibility/generalization of these formats with serde/miniserde, etc isn't necessary for a simple message dictionary, given their additional cost if we were to make them necessary for all of the formats. Something like postcard or lite-json could also be good.

@kellpossible
Copy link
Owner Author

Having support for converting between the formats at compile time would also be good, so the data can be stored in the repository an any format, and converted to a format that meets the needs of the application at runtime. For embedded this could mean having a wider range of repo storage formats, and on device a nice slim binary format.

@kellpossible
Copy link
Owner Author

kellpossible commented Feb 20, 2022

I think the interface should be mostly covered by traits, and left to the examples for how to implement them. Maybe have a default implementation for serde and ufmt.

For each of the data formats, something like this:

trait FmtLocalizationDictionary<'m> {
    fn get(message_id: &str) -> &'m str;
}

/// Not needed for no_std, useful for conversions during build and for editing translations.
trait FmtLocalizationDictionaryMut<'m>: FmtLocalizationDictionary<'m> {
    fn set(message_id: &str, new_message: String) -> String;
}

For each of the formatting methods, maybe something like this?

trait MessageFormatter {
    fn format_message(message: &str) -> String;
}

Could also get some inspiration from the generic formatter design in ufmt.

Ideally the client will be requesting the formatter via some API where they can pass in a fixed size buffer from the stack to receive the results of the localized message.

Perhaps this generic message formatting support and decoupling from storage formats will link in well with #42

@kellpossible kellpossible added the enhancement New feature or request label Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant