@@ -25,6 +25,7 @@ use std::fs;
25
25
use std:: fs:: File ;
26
26
use std:: io;
27
27
use std:: io:: prelude:: * ;
28
+ use std:: path:: Path ;
28
29
use std:: str:: FromStr ;
29
30
use unic_langid:: { langid, LanguageIdentifier } ;
30
31
@@ -33,7 +34,7 @@ use unic_langid::{langid, LanguageIdentifier};
33
34
///
34
35
/// The resource files are stored in
35
36
/// `./examples/resources/{locale}` directory.
36
- fn read_file ( path : & str ) -> Result < String , io:: Error > {
37
+ fn read_file ( path : & Path ) -> Result < String , io:: Error > {
37
38
let mut f = File :: open ( path) ?;
38
39
let mut s = String :: new ( ) ;
39
40
f. read_to_string ( & mut s) ?;
@@ -49,14 +50,17 @@ fn read_file(path: &str) -> Result<String, io::Error> {
49
50
fn get_available_locales ( ) -> Result < Vec < LanguageIdentifier > , io:: Error > {
50
51
let mut locales = vec ! [ ] ;
51
52
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) ?;
53
57
for entry in res_dir {
54
58
if let Ok ( entry) = entry {
55
59
let path = entry. path ( ) ;
56
60
if path. is_dir ( ) {
57
61
if let Some ( name) = path. file_name ( ) {
58
62
if let Some ( name) = name. to_str ( ) {
59
- let langid: LanguageIdentifier = name. parse ( ) . expect ( "Parsing failed." ) ;
63
+ let langid = name. parse ( ) . expect ( "Parsing failed." ) ;
60
64
locales. push ( langid) ;
61
65
}
62
66
}
@@ -100,11 +104,11 @@ fn main() {
100
104
101
105
// 6. Load the localization resource
102
106
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 ) ;
108
112
let source = read_file ( & full_path) . expect ( "Failed to read file." ) ;
109
113
let resource = FluentResource :: try_new ( source) . expect ( "Could not parse an FTL string." ) ;
110
114
bundle
0 commit comments