Comments (3)
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.
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.
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)
- ResourceManager::get_bundle and get_bundles is inconsistent and confusing
- ResourceManager ignores and hides errors
- ResourceManager needs to implement BundleGenerator HOT 2
- Parser swallows leading whitespace after indented placeable HOT 2
- Clippy should be run in CI HOT 2
- rustmt should be run in CI
- error[E0277]: `(dyn Any + 'static)` cannot be sent between threads safely HOT 11
- Cannot use built-in functions HOT 2
- Which data providers Fluent crates use? HOT 3
- Make FluentArgs a trait? HOT 1
- Switching windows process/thread ui languages HOT 2
- Provide Way to Avoid Allocating To Collect Formatting Errors HOT 7
- Using or switching to icu4x crates? HOT 4
- Behaviour of FluentArgs::set is misleading HOT 1
- Support full precision of all numbers HOT 1
- How to use this in a command line program and properly parse POSIX locales? HOT 1
- [Feature request] Allow serialize all AST types instead of just Resource HOT 7
- Include source position in the AST HOT 5
- Does a 'safe harbor' release of the current HEAD make sense before further changes? HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluent-rs.