Giter Club home page Giter Club logo

Comments (3)

sverro2 avatar sverro2 commented on June 6, 2024

Leaving the code here to copy/look at:

use fluent::{FluentResource, FluentBundle, FluentArgs, FluentValue};
use unic_langid::LanguageIdentifier;

fn main() {
    let ftl_string = String::from(r#"
just-url-dynamic = content before link<a href="{$url}">this is a url</a>content after link
link-dynamic = content before link{$url}content after link"#);
    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.");

    // Testing what should be two identical ftl outputs strings.

    // Starting with a sentence with a url passed in. 
    let mut just_url_dynamic_arg = FluentArgs::new();
    just_url_dynamic_arg.set("url", FluentValue::from("https://projectfluent.org/"));
    
    let msg = bundle.get_message("just-url-dynamic")
        .expect("Message doesn't exist.");
    let mut errors = vec![];
    let pattern = msg.value().expect("Message has no value.");
    let just_url_dynamic_value = bundle.format_pattern(&pattern, Some(&just_url_dynamic_arg), &mut errors);

    // Then instead of passing in the url, pass in the entire html link.
    let mut link_dynamic_arg = FluentArgs::new();
    link_dynamic_arg.set("url", FluentValue::from(r#"<a href="https://projectfluent.org/">this is a url</a>"#));

    let msg = bundle.get_message("link-dynamic")
    .expect("Message doesn't exist.");
    let mut errors = vec![];
    let pattern = msg.value().expect("Message has no value.");
    let link_dynamic_value = bundle.format_pattern(&pattern, Some(&link_dynamic_arg), &mut errors);
    
    // both indeed look the same.
    println!("Compare these too, they are the same right?");
    println!("{link_dynamic_value}\n{just_url_dynamic_value}");
    println!("But are they? See for yourself: {}", link_dynamic_value == just_url_dynamic_value);
    println!("Just try to open both urls in your browser.");

    // They aren't the same. What does it actually look like?
    println!("\n\nByte arrays:\n{:?}\n{:?}\n\n", link_dynamic_value.as_bytes(), just_url_dynamic_value.as_bytes());

    // YUP they indeed are different!
}

from fluent-rs.

sverro2 avatar sverro2 commented on June 6, 2024

This has to do with Unicode Isolation. I feel a bit stupid. But maybe the documentation about this can be a bit more clear/easier to find to prevent some other people from wasting time. For instance, in https://github.com/projectfluent/fluent.js/wiki/Unicode-Isolation (which took me some time to find, because I didn't know Unicode Isolation was the issue) it describes how to enable/disable the functionality, but it doesn't say that the URL in the Arabic translation would look great with isolation enabled, but won't actually work when trying to use it in an HTML link!

How did I get to know what the problem was? On the fluent-templates pages, all the way at the bottom (the FAQ section), I finally got the clue that confirmed what the real problem was:
https://crates.io/crates/fluent-templates - The FAQ section.

from fluent-rs.

zbraniecki avatar zbraniecki commented on June 6, 2024

Hi!

As a person who recently encountered this issue, do you have a suggestion where this should be documented and how?
I'd accept a PR with a suggestion

from fluent-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.