[−][src]Struct fluent::bundle::FluentBundleBase
Base class for a FluentBundle
struct. See its docs for details.
It also is implemented for concurrent::FluentBundle
.
Fields
locales: Vec<LanguageIdentifier>
Implementations
impl<R, M> FluentBundleBase<R, M> where
M: MemoizerKind,
[src]
M: MemoizerKind,
pub fn new<'a, L>(
locales: impl IntoIterator<Item = &'a L>
) -> FluentBundleBase<R, M> where
L: 'a + Clone + PartialEq<L> + Into<LanguageIdentifier>,
[src]
locales: impl IntoIterator<Item = &'a L>
) -> FluentBundleBase<R, M> where
L: 'a + Clone + PartialEq<L> + Into<LanguageIdentifier>,
Constructs a FluentBundle. locales
is the fallback chain of locales
to use for formatters like date and time. locales
does not influence
message selection.
Examples
use fluent_bundle::FluentBundle; use fluent_bundle::FluentResource; use unic_langid::langid; let langid_en = langid!("en-US"); let mut bundle: FluentBundle<FluentResource> = FluentBundle::new(&[langid_en]);
Errors
This will panic if no formatters can be found for the locales.
pub fn add_resource(&mut self, r: R) -> Result<(), Vec<FluentError>> where
R: Borrow<FluentResource>,
[src]
R: Borrow<FluentResource>,
Adds a resource to the bundle, returning an empty Result<T>
on success.
If any entry in the resource uses the same identifier as an already
existing key in the bundle, the new entry will be ignored and a
FluentError::Overriding
will be added to the result.
The method can take any type that can be borrowed to FluentResource:
- FluentResource
- &FluentResource
- Rc
- Arc
This allows the user to introduce custom resource management and share
resources between instances of FluentBundle
.
Examples
use fluent_bundle::{FluentBundle, FluentResource}; use unic_langid::langid; let ftl_string = String::from(" hello = Hi! goodbye = Bye! "); let resource = FluentResource::try_new(ftl_string) .expect("Could not parse an FTL string."); let langid_en = langid!("en-US"); let mut bundle = FluentBundle::new(&[langid_en]); bundle.add_resource(resource) .expect("Failed to add FTL resources to the bundle."); assert_eq!(true, bundle.has_message("hello"));
Whitespace
Message ids must have no leading whitespace. Message values that span
multiple lines must have leading whitespace on all but the first line. These
are standard FTL syntax rules that may prove a bit troublesome in source
code formatting. The indoc!
crate can help with stripping extra indentation
if you wish to indent your entire message.
pub fn add_resource_overriding(&mut self, r: R) where
R: Borrow<FluentResource>,
[src]
R: Borrow<FluentResource>,
Adds a resource to the bundle, returning an empty Result<T>
on success.
If any entry in the resource uses the same identifier as an already existing key in the bundle, the entry will override the previous one.
The method can take any type that can be borrowed as FluentResource:
- FluentResource
- &FluentResource
- Rc
- Arc
This allows the user to introduce custom resource management and share
resources between instances of FluentBundle
.
Examples
use fluent_bundle::{FluentBundle, FluentResource}; use unic_langid::langid; let ftl_string = String::from(" hello = Hi! goodbye = Bye! "); let resource = FluentResource::try_new(ftl_string) .expect("Could not parse an FTL string."); let ftl_string = String::from(" hello = Another Hi! "); let resource2 = FluentResource::try_new(ftl_string) .expect("Could not parse an FTL string."); let langid_en = langid!("en-US"); let mut bundle = FluentBundle::new(&[langid_en]); bundle.add_resource(resource) .expect("Failed to add FTL resources to the bundle."); bundle.add_resource_overriding(resource2); let mut errors = vec![]; let msg = bundle.get_message("hello") .expect("Failed to retrieve the message"); let value = msg.value.expect("Failed to retrieve the value of the message"); assert_eq!(bundle.format_pattern(value, None, &mut errors), "Another Hi!");
Whitespace
Message ids must have no leading whitespace. Message values that span
multiple lines must have leading whitespace on all but the first line. These
are standard FTL syntax rules that may prove a bit troublesome in source
code formatting. The indoc!
crate can help with stripping extra indentation
if you wish to indent your entire message.
pub fn set_use_isolating(&mut self, value: bool)
[src]
When formatting patterns, FluentBundle
inserts
Unicode Directionality Isolation Marks to indicate
that the direction of a placeable may differ from
the surrounding message.
This is important for cases such as when a right-to-left user name is presented in the left-to-right message.
In some cases, such as testing, the user may want to disable the isolating.
pub fn set_transform(&mut self, func: Option<fn(&str) -> Cow<'_, str>>)
[src]
This method allows to specify a function that will be called on all textual fragments of the pattern during formatting.
This is currently primarly used for pseudolocalization,
and fluent-pseudo
crate provides a function
that can be passed here.
pub fn set_formatter(
&mut self,
func: Option<fn(&FluentValue<'_>, &M) -> Option<String>>
)
[src]
&mut self,
func: Option<fn(&FluentValue<'_>, &M) -> Option<String>>
)
This method allows to specify a function that will
be called before any FluentValue
is formatted
allowing overrides.
It's particularly useful for plugging in an external
formatter for FluentValue::Number
.
pub fn has_message(&self, id: &str) -> bool where
R: Borrow<FluentResource>,
[src]
R: Borrow<FluentResource>,
Returns true if this bundle contains a message with the given id.
Examples
use fluent_bundle::{FluentBundle, FluentResource}; use unic_langid::langid; let ftl_string = String::from("hello = Hi!"); let resource = FluentResource::try_new(ftl_string) .expect("Failed to parse an FTL string."); let langid_en = langid!("en-US"); let mut bundle = FluentBundle::new(&[langid_en]); bundle.add_resource(&resource) .expect("Failed to add FTL resources to the bundle."); assert_eq!(true, bundle.has_message("hello"));
pub fn get_message(&self, id: &str) -> Option<FluentMessage<'_>> where
R: Borrow<FluentResource>,
[src]
R: Borrow<FluentResource>,
Retrieves a FluentMessage
from a bundle.
Examples
use fluent_bundle::{FluentBundle, FluentResource}; use unic_langid::langid; let ftl_string = String::from("hello-world = Hello World!"); let resource = FluentResource::try_new(ftl_string) .expect("Failed to parse an FTL string."); let langid_en = langid!("en-US"); let mut bundle = FluentBundle::new(&[langid_en]); bundle.add_resource(&resource) .expect("Failed to add FTL resources to the bundle."); let msg = bundle.get_message("hello-world"); assert_eq!(msg.is_some(), true);
pub fn write_pattern<W>(
&'bundle self,
w: &mut W,
pattern: &'bundle Pattern<&str>,
args: Option<&'bundle FluentArgs<'_>>,
errors: &mut Vec<FluentError>
) -> Result<(), Error> where
R: Borrow<FluentResource>,
W: Write,
[src]
&'bundle self,
w: &mut W,
pattern: &'bundle Pattern<&str>,
args: Option<&'bundle FluentArgs<'_>>,
errors: &mut Vec<FluentError>
) -> Result<(), Error> where
R: Borrow<FluentResource>,
W: Write,
Writes a formatted pattern which comes from a FluentMessage
.
Example
use fluent_bundle::{FluentBundle, FluentResource}; use unic_langid::langid; let ftl_string = String::from("hello-world = Hello World!"); let resource = FluentResource::try_new(ftl_string) .expect("Failed to parse an FTL string."); let langid_en = langid!("en-US"); let mut bundle = FluentBundle::new(&[langid_en]); bundle.add_resource(&resource) .expect("Failed to add FTL resources to the bundle."); let msg = bundle.get_message("hello-world") .expect("Failed to retrieve a FluentMessage."); let pattern = msg.value .expect("Missing Value."); let mut errors = vec![]; let mut s = String::new(); bundle.write_pattern(&mut s, &pattern, None, &mut errors) .expect("Failed to write."); assert_eq!(s, "Hello World!");
pub fn format_pattern(
&'bundle self,
pattern: &'bundle Pattern<&str>,
args: Option<&'bundle FluentArgs<'_>>,
errors: &mut Vec<FluentError>
) -> Cow<'bundle, str> where
R: Borrow<FluentResource>,
[src]
&'bundle self,
pattern: &'bundle Pattern<&str>,
args: Option<&'bundle FluentArgs<'_>>,
errors: &mut Vec<FluentError>
) -> Cow<'bundle, str> where
R: Borrow<FluentResource>,
Formats a pattern which comes from a FluentMessage
.
Example
use fluent_bundle::{FluentBundle, FluentResource}; use unic_langid::langid; let ftl_string = String::from("hello-world = Hello World!"); let resource = FluentResource::try_new(ftl_string) .expect("Failed to parse an FTL string."); let langid_en = langid!("en-US"); let mut bundle = FluentBundle::new(&[langid_en]); bundle.add_resource(&resource) .expect("Failed to add FTL resources to the bundle."); let msg = bundle.get_message("hello-world") .expect("Failed to retrieve a FluentMessage."); let pattern = msg.value .expect("Missing Value."); let mut errors = vec![]; let result = bundle.format_pattern(&pattern, None, &mut errors); assert_eq!(result, "Hello World!");
pub fn add_function<F>(&mut self, id: &str, func: F) -> Result<(), FluentError> where
F: for<'a> Fn(&[FluentValue<'a>], &FluentArgs<'_>) -> FluentValue<'a> + Sync + Send + 'static,
[src]
F: for<'a> Fn(&[FluentValue<'a>], &FluentArgs<'_>) -> FluentValue<'a> + Sync + Send + 'static,
Makes the provided rust function available to messages with the name id
. See
the FTL syntax guide to learn how these are used in messages.
FTL functions accept both positional and named args. The rust function you
provide therefore has two parameters: a slice of values for the positional
args, and a FluentArgs
for named args.
Examples
use fluent_bundle::{FluentBundle, FluentResource, FluentValue}; use unic_langid::langid; let ftl_string = String::from("length = { STRLEN(\"12345\") }"); let resource = FluentResource::try_new(ftl_string) .expect("Could not parse an FTL string."); let langid_en = langid!("en-US"); let mut bundle = FluentBundle::new(&[langid_en]); bundle.add_resource(&resource) .expect("Failed to add FTL resources to the bundle."); // Register a fn that maps from string to string length bundle.add_function("STRLEN", |positional, _named| match positional { [FluentValue::String(str)] => str.len().into(), _ => FluentValue::Error, }).expect("Failed to add a function to the bundle."); let msg = bundle.get_message("length").expect("Message doesn't exist."); let mut errors = vec![]; let pattern = msg.value.expect("Message has no value."); let value = bundle.format_pattern(&pattern, None, &mut errors); assert_eq!(&value, "5");
Trait Implementations
impl<R, M> Default for FluentBundleBase<R, M> where
M: MemoizerKind,
[src]
M: MemoizerKind,
fn default() -> FluentBundleBase<R, M>
[src]
Auto Trait Implementations
impl<R, M> !RefUnwindSafe for FluentBundleBase<R, M>
impl<R, M> Send for FluentBundleBase<R, M> where
M: Send,
R: Send,
M: Send,
R: Send,
impl<R, M> Sync for FluentBundleBase<R, M> where
M: Sync,
R: Sync,
M: Sync,
R: Sync,
impl<R, M> Unpin for FluentBundleBase<R, M> where
M: Unpin,
R: Unpin,
M: Unpin,
R: Unpin,
impl<R, M> !UnwindSafe for FluentBundleBase<R, M>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,