Skip to content

Commit 4c608e6

Browse files
committed
Improve the simple-app example
1 parent 664a0a7 commit 4c608e6

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

fluent-bundle/examples/simple-app.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::fs;
2525
use std::fs::File;
2626
use std::io;
2727
use std::io::prelude::*;
28+
use std::path::Path;
2829
use std::str::FromStr;
2930
use unic_langid::{langid, LanguageIdentifier};
3031

@@ -33,7 +34,7 @@ use unic_langid::{langid, LanguageIdentifier};
3334
///
3435
/// The resource files are stored in
3536
/// `./examples/resources/{locale}` directory.
36-
fn read_file(path: &str) -> Result<String, io::Error> {
37+
fn read_file(path: &Path) -> Result<String, io::Error> {
3738
let mut f = File::open(path)?;
3839
let mut s = String::new();
3940
f.read_to_string(&mut s)?;
@@ -49,14 +50,17 @@ fn read_file(path: &str) -> Result<String, io::Error> {
4950
fn get_available_locales() -> Result<Vec<LanguageIdentifier>, io::Error> {
5051
let mut locales = vec![];
5152

52-
let res_dir = fs::read_dir("./fluent-bundle/examples/resources/")?;
53+
let mut dir = env::current_dir()?;
54+
dir.push("examples");
55+
dir.push("resources");
56+
let res_dir = fs::read_dir(dir)?;
5357
for entry in res_dir {
5458
if let Ok(entry) = entry {
5559
let path = entry.path();
5660
if path.is_dir() {
5761
if let Some(name) = path.file_name() {
5862
if let Some(name) = name.to_str() {
59-
let langid: LanguageIdentifier = name.parse().expect("Parsing failed.");
63+
let langid = name.parse().expect("Parsing failed.");
6064
locales.push(langid);
6165
}
6266
}
@@ -100,11 +104,11 @@ fn main() {
100104

101105
// 6. Load the localization resource
102106
for path in L10N_RESOURCES {
103-
let full_path = format!(
104-
"./fluent-bundle/examples/resources/{locale}/{path}",
105-
locale = current_locale,
106-
path = path
107-
);
107+
let mut full_path = env::current_dir().expect("Failed to retireve current dir.");
108+
full_path.push("examples");
109+
full_path.push("resources");
110+
full_path.push(current_locale.to_string());
111+
full_path.push(path);
108112
let source = read_file(&full_path).expect("Failed to read file.");
109113
let resource = FluentResource::try_new(source).expect("Could not parse an FTL string.");
110114
bundle

fluent-bundle/src/bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use std::default::Default;
1212
use fluent_locale::{negotiate_languages, NegotiationStrategy};
1313
use fluent_syntax::ast;
1414
use intl_pluralrules::{IntlPluralRules, PluralRuleType};
15-
use unic_langid::LanguageIdentifier;
1615
use unic_langid::langid;
16+
use unic_langid::LanguageIdentifier;
1717

1818
use crate::entry::Entry;
1919
use crate::entry::GetEntry;

fluent-bundle/tests/types_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn fluent_value_number() {
1414
fn fluent_value_matches() {
1515
// We'll use `ars` locale since it happens to have all
1616
// plural rules categories.
17-
let langid_ars = langid!("ars");
17+
let langid_ars = langid!("ars");
1818
let bundle: FluentBundle<FluentResource> = FluentBundle::new(&[langid_ars]);
1919
let scope = Scope::new(&bundle, None);
2020

0 commit comments

Comments
 (0)