[][src]Struct fluent_ergonomics::FluentErgo

pub struct FluentErgo { /* fields omitted */ }

Implementations

impl FluentErgo[src]

An Ergonomic class wrapping the Fluent library

pub fn new(languages: &[LanguageIdentifier]) -> FluentErgo[src]

Construct the class with a list of languages. The list must be sorted in the order that language packs will be tested. The first language listed will be the first language searched for any translation message.

Typically, I call this as

let eo_id = "eo".parse::<unic_langid::LanguageIdentifier>().unwrap();
let en_id = "en-US".parse::<unic_langid::LanguageIdentifier>().unwrap();

let mut fluent = fluent_ergonomics::FluentErgo::new(&[eo_id, en_id]);

This specifies that I want to first look up messages in the Esperanto list, then fall back to the English specfications if no Esperanto specification is present.

Note that no language resources are loaded during construction. You must call add_from_text or add_from_file to load language packs.

pub fn add_from_text(
    &mut self,
    lang: LanguageIdentifier,
    text: String
) -> Result<(), Error>
[src]

Add a list of translation strings from a string, which can be a constant hard-coded in the application, loaded from a file, loaded from the internet, or wherever you like. lang specifies which language the translation strings being provided.

You should not specify a language that you did not include in the constructor. You can, but the translation function will only check those languages specified when this object was constructed.

Errors

  • FluentError
  • FluentParserError

pub fn add_from_file(
    &mut self,
    lang: LanguageIdentifier,
    path: &Path
) -> Result<(), Error>
[src]

Like add_from_text, but this will load the translation strings from a file.

Note that this will load the entire file into memory before passing it to Fluent. While I think it is unlikely, it is possible that a translation file may be so big as to run the computer out of memory.

Errors

  • FluentError
  • FluentParserError
  • FileEncodingError -- all files must be encoded in UTF-8. Most files saved from text editors already do proper UTF-8 encoding, so this should rarely be a problem.

pub fn tr(
    &self,
    msgid: &str,
    args: Option<&FluentArgs>
) -> Result<String, Error>
[src]

Run a translation.

msgid is the translation identifier as specified in the translation strings. args is a set of Fluent arguments to be interpolated into the strings.

This function will search language bundles in the order that they were specified in the constructor. NoMatchingMessage will be returned only if the message identifier cannot be found in any bundle.

This example is not tested
length-without-label = {$value}
swimming = Swimming
units = Units

With this set of translation strings, length-without-label, swimming, and units are all valid translation identifiers. See the documentation for FluentBundle.get_message for more information.

A typical call with arguments would look like this:

use fluent::{FluentArgs, FluentValue};

let eo_id = "eo".parse::<unic_langid::LanguageIdentifier>().unwrap();
let en_id = "en-US".parse::<unic_langid::LanguageIdentifier>().unwrap();

let mut fluent = fluent_ergonomics::FluentErgo::new(&[eo_id, en_id]);
let mut args = FluentArgs::new();
args.insert("value", FluentValue::from("15"));
let r = fluent.tr("length-without-label", Some(&args));

Errors

  • NoMatchingMessage -- this will be returned if the message identifier cannot be found in any language bundle.

Trait Implementations

impl Clone for FluentErgo[src]

impl Debug for FluentErgo[src]

impl Default for FluentErgo[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.