Skip to content

Commit 49ff95d

Browse files
authored
Rollup merge of #121065 - CAD97:display-i18n, r=cuviper
Add basic i18n guidance for `Display` I've tried to be relatively noncommittal here. The part I think is most important is to mention the concept of "display adapters" *somewhere* in the `std::fmt` documentation that has some chance of being discovered when people go looking for ways to provide context when `Display`ing their type. Rendered: > ### Internationalization > > Because a type can only have one `Display` implementation, it is often preferable to only implement `Display` when there is a single most "obvious" way that values can be formatted as text. This could mean formatting according to the "invariant" culture and "undefined" locale, or it could mean that the type display is designed for a specific culture/locale, such as developer logs. > > If not all values have a justifiably canonical textual format or if you want to support alternative formats not covered by the standard set of possible [formatting traits], the most flexible approach is display adapters: methods like [`str::escape_default`] or [`Path::display`] which create a wrapper implementing `Display` to output the specific display format. > > [formatting traits]: https://doc.rust-lang.org/nightly/std/fmt/index.html#formatting-traits > [`str::escape_default`]: https://doc.rust-lang.org/nightly/std/primitive.str.html#method.escape_default > [`Path::display`]: https://doc.rust-lang.org/nightly/std/path/struct.Path.html#method.display The module docs do already have a [localization header](https://doc.rust-lang.org/nightly/std/fmt/index.html#localization), so maybe this header should be l10n instead of i18n, or maybe this information should live under that header? I'm not sure, but here on the `Display` trait at least isn't a *bad* spot to put it. The other side of this that comes up a lot is `FromStr` compatibility, but that's for a different PR.
2 parents 3c02972 + 215a4b6 commit 49ff95d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

library/core/src/fmt/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,23 @@ pub use macros::Debug;
633633
/// [tostring]: ../../std/string/trait.ToString.html
634634
/// [tostring_function]: ../../std/string/trait.ToString.html#tymethod.to_string
635635
///
636+
/// # Internationalization
637+
///
638+
/// Because a type can only have one `Display` implementation, it is often preferable
639+
/// to only implement `Display` when there is a single most "obvious" way that
640+
/// values can be formatted as text. This could mean formatting according to the
641+
/// "invariant" culture and "undefined" locale, or it could mean that the type
642+
/// display is designed for a specific culture/locale, such as developer logs.
643+
///
644+
/// If not all values have a justifiably canonical textual format or if you want
645+
/// to support alternative formats not covered by the standard set of possible
646+
/// [formatting traits], the most flexible approach is display adapters: methods
647+
/// like [`str::escape_default`] or [`Path::display`] which create a wrapper
648+
/// implementing `Display` to output the specific display format.
649+
///
650+
/// [formatting traits]: ../../std/fmt/index.html#formatting-traits
651+
/// [`Path::display`]: ../../std/path/struct.Path.html#method.display
652+
///
636653
/// # Examples
637654
///
638655
/// Implementing `Display` on a type:

0 commit comments

Comments
 (0)