projectfluent / fluent-rs

Rust implementation of Project Fluent
https://projectfluent.org
Apache License 2.0
1.08k stars 97 forks source link

Example does not work #226

Closed makorne closed 3 years ago

makorne commented 3 years ago
use fluent::{FluentBundle, FluentValue, FluentResource, FluentArgs};

// Used to provide a locale for the bundle.
use unic_langid::LanguageIdentifier;

fn main() {
    let ftl_string = String::from("
    hello-world = Hello, world!
    intro = Welcome, { $name }.
    ");
    let res = FluentResource::try_new(ftl_string)
    .expect("Failed to parse an FTL string.");

    let langid_en: LanguageIdentifier = "en-US".parse().expect("Parsing failed");
    let mut bundle = FluentBundle::new(vec![langid_en]);

    bundle
    .add_resource(res)
    .expect("Failed to add FTL resources to the bundle.");

    let msg = bundle.get_message("hello-world")
    .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, "Hello, world!");

    let mut args = FluentArgs::new();
    args.set("name", FluentValue::from("John"));

    let msg = bundle.get_message("intro")
    .expect("Message doesn't exist.");
    let mut errors = vec![];
    let pattern = msg.value().expect("Message has no value.");
    let value = bundle.format_pattern(&pattern, Some(&args), &mut errors);

    // The FSI/PDI isolation marks ensure that the direction of
    // the text from the variable is not affected by the translation.
    assert_eq!(value, "Welcome, \u{2068}John\u{2069}.");
}
thread 'main' panicked at 'Failed to parse an FTL string.: (FluentResource(InnerFluentResource { ast: Resource { body: [Junk { content: "    hello-world = Hello, world!\n    intro = Welcome, { $name }.\n    " }] }, string: "\n    hello-world = Hello, world!\n    intro = Welcome, { $name }.\n    " }), [ParserError { pos: 1..2, slice: Some(1..69), kind: ExpectedCharRange { range: "a-zA-Z" } }])', src/main.rs:12:6
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/core/src/panicking.rs:92:14
   2: core::option::expect_none_failed
             at /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/core/src/option.rs:1329:5
   3: core::result::Result<T,E>::expect
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:997:23
   4: Test::main
             at ./src/main.rs:11:15
   5: core::ops::function::FnOnce::call_once
             at /home/f/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
zbraniecki commented 3 years ago

hi. Your FTL source is invalid. Entries need to start on a new line, and your multiline string has indentation:

    let ftl_string = String::from("
    hello-world = Hello, world!
    intro = Welcome, { $name }.
    ");

Try:

    let ftl_string = String::from("
hello-world = Hello, world!
intro = Welcome, { $name }.
");
zbraniecki commented 3 years ago

reopen if needed.