|
| 1 | +diff --git a/fluent-bundle/src/bundle.rs b/fluent-bundle/src/bundle.rs |
| 2 | +index ba98774..fb595b0 100644 |
| 3 | +--- a/fluent-bundle/src/bundle.rs |
| 4 | ++++ b/fluent-bundle/src/bundle.rs |
| 5 | +@@ -386,7 +386,7 @@ impl<R, M> FluentBundle<R, M> { |
| 6 | + /// let msg = bundle.get_message("hello-world"); |
| 7 | + /// assert_eq!(msg.is_some(), true); |
| 8 | + /// ``` |
| 9 | +- pub fn get_message(&self, id: &str) -> Option<FluentMessage<&str>> |
| 10 | ++ pub fn get_message(&self, id: &str) -> Option<FluentMessage> |
| 11 | + where |
| 12 | + R: Borrow<FluentResource>, |
| 13 | + { |
| 14 | +diff --git a/fluent-bundle/src/lib.rs b/fluent-bundle/src/lib.rs |
| 15 | +index edfc2b3..04a093d 100644 |
| 16 | +--- a/fluent-bundle/src/lib.rs |
| 17 | ++++ b/fluent-bundle/src/lib.rs |
| 18 | +@@ -8,6 +8,11 @@ |
| 19 | + //! |
| 20 | + //! There are four core concepts to understand Fluent runtime: |
| 21 | + //! |
| 22 | ++//! * [`FluentMessage`] - A single translation unit |
| 23 | ++//! * [`FluentResource`] - A list of [`FluentMessage`] units |
| 24 | ++//! * [`FluentBundle`] - A collection of [`FluentResource`] lists |
| 25 | ++//! * [`FluentArgs`] - A list of [`FluentValue`] elements used to resolve a [`FluentMessage`] value |
| 26 | ++//! |
| 27 | + //! # 1) Message |
| 28 | + //! |
| 29 | + //! [`FluentMessage`] is the core unit of the system. |
| 30 | +diff --git a/fluent-bundle/src/message.rs b/fluent-bundle/src/message.rs |
| 31 | +index 93c3487..284fb4b 100644 |
| 32 | +--- a/fluent-bundle/src/message.rs |
| 33 | ++++ b/fluent-bundle/src/message.rs |
| 34 | +@@ -1,38 +1,70 @@ |
| 35 | +-use fluent_syntax::{ast, parser::Slice}; |
| 36 | ++use fluent_syntax::ast; |
| 37 | + |
| 38 | + #[derive(Debug, PartialEq)] |
| 39 | +-pub struct FluentAttribute<'m, S> { |
| 40 | +- pub id: &'m S, |
| 41 | +- pub value: &'m ast::Pattern<S>, |
| 42 | ++pub struct FluentAttribute<'m> { |
| 43 | ++ pub id: &'m str, |
| 44 | ++ pub value: &'m ast::Pattern<&'m str>, |
| 45 | + } |
| 46 | + |
| 47 | +-impl<'m, S> From<&'m ast::Attribute<S>> for FluentAttribute<'m, S> { |
| 48 | +- fn from(attr: &'m ast::Attribute<S>) -> Self { |
| 49 | ++impl<'m> From<&'m ast::Attribute<&'m str>> for FluentAttribute<'m> { |
| 50 | ++ fn from(attr: &'m ast::Attribute<&'m str>) -> Self { |
| 51 | + FluentAttribute { |
| 52 | +- id: &attr.id.name, |
| 53 | ++ id: attr.id.name, |
| 54 | + value: &attr.value, |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +-/// A single localization unit composed of an identifier, |
| 59 | +-/// value, and attributes. |
| 60 | ++ |
| 61 | ++/// [`FluentMessage`] is a basic translation unit of the Fluent system. |
| 62 | ++/// |
| 63 | ++/// A message is composed of a value and, optionally a list of attributes. |
| 64 | ++/// |
| 65 | ++/// ### Simple Message |
| 66 | ++/// |
| 67 | ++/// ``` |
| 68 | ++/// use fluent_bundle::{FluentResource, FluentBundle}; |
| 69 | ++/// |
| 70 | ++/// let source = r#" |
| 71 | ++/// |
| 72 | ++/// hello-world = Hello, ${ user } |
| 73 | ++/// |
| 74 | ++/// "#; |
| 75 | ++/// |
| 76 | ++/// let resource = FluentResource::try_new(source.to_string()) |
| 77 | ++/// .expect("Failed to parse the resource."); |
| 78 | ++/// |
| 79 | ++/// let mut bundle = FluentBundle::default(); |
| 80 | ++/// bundle.add_resource(resource) |
| 81 | ++/// .expect("Failed to add a resource."); |
| 82 | ++/// |
| 83 | ++/// let msg = bundle.get_message("hello-world") |
| 84 | ++/// .expect("Failed to retrieve a message."); |
| 85 | ++/// |
| 86 | ++/// assert!(msg.value.is_some()); |
| 87 | ++/// ``` |
| 88 | ++/// |
| 89 | ++/// ### Compound Message |
| 90 | ++/// |
| 91 | ++/// ```text |
| 92 | ++/// confirm-modal = Are you sure? |
| 93 | ++/// .confirm = Yes |
| 94 | ++/// .cancel = No |
| 95 | ++/// .tooltip = Closing the window will lose all unsaved data. |
| 96 | ++/// ``` |
| 97 | + #[derive(Debug, PartialEq)] |
| 98 | +-pub struct FluentMessage<'m, S> { |
| 99 | +- pub value: Option<&'m ast::Pattern<S>>, |
| 100 | +- pub attributes: Vec<FluentAttribute<'m, S>>, |
| 101 | ++pub struct FluentMessage<'m> { |
| 102 | ++ pub value: Option<&'m ast::Pattern<&'m str>>, |
| 103 | ++ pub attributes: Vec<FluentAttribute<'m>>, |
| 104 | + } |
| 105 | + |
| 106 | +-impl<'m, S> FluentMessage<'m, S> { |
| 107 | +- pub fn get_attribute(&self, key: &str) -> Option<&FluentAttribute<S>> |
| 108 | +- where |
| 109 | +- S: Slice<'m>, |
| 110 | +- { |
| 111 | +- self.attributes.iter().find(|attr| attr.id.as_ref() == key) |
| 112 | ++impl<'m> FluentMessage<'m> { |
| 113 | ++ pub fn get_attribute(&self, key: &str) -> Option<&FluentAttribute> { |
| 114 | ++ self.attributes.iter().find(|attr| attr.id == key) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +-impl<'m, S> From<&'m ast::Message<S>> for FluentMessage<'m, S> { |
| 119 | +- fn from(msg: &'m ast::Message<S>) -> Self { |
| 120 | ++impl<'m> From<&'m ast::Message<&'m str>> for FluentMessage<'m> { |
| 121 | ++ fn from(msg: &'m ast::Message<&'m str>) -> Self { |
| 122 | + FluentMessage { |
| 123 | + value: msg.value.as_ref(), |
| 124 | + attributes: msg.attributes.iter().map(Into::into).collect(), |
0 commit comments