From 944cba030e3a81c6db2f19604cf9b66ff06c6ebb Mon Sep 17 00:00:00 2001 From: Basix Date: Wed, 11 Nov 2020 19:12:24 +0900 Subject: [PATCH] Allow to specify default language in filenames (#1233) * Use default language if code is same as default * Add test for specifying default locale * Update docs to mention default languages --- components/library/src/content/file_info.rs | 18 ++++++++++++++++++ .../documentation/content/multilingual.md | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/components/library/src/content/file_info.rs b/components/library/src/content/file_info.rs index 7cae3c11..167d6048 100644 --- a/components/library/src/content/file_info.rs +++ b/components/library/src/content/file_info.rs @@ -129,6 +129,11 @@ impl FileInfo { // We can document that let mut parts: Vec = self.name.splitn(2, '.').map(|s| s.to_string()).collect(); + // If language code is same as default language, go for default + if config.default_language == parts[1].as_str() { + return Ok(config.default_language.clone()); + } + // The language code is not present in the config: typo or the user forgot to add it to the // config if !config.languages_codes().contains(&parts[1].as_ref()) { @@ -189,6 +194,19 @@ mod tests { assert_eq!(res.unwrap(), "fr"); } + #[test] + fn can_find_valid_language_with_default_locale() { + let mut config = Config::default(); + config.languages.push(Language { code: String::from("fr"), feed: false, search: false }); + let mut file = FileInfo::new_page( + &Path::new("/home/vincent/code/site/content/posts/tutorials/python.en.md"), + &PathBuf::new(), + ); + let res = file.find_language(&config); + assert!(res.is_ok()); + assert_eq!(res.unwrap(), config.default_language); + } + #[test] fn can_find_valid_language_in_page_with_assets() { let mut config = Config::default(); diff --git a/docs/content/documentation/content/multilingual.md b/docs/content/documentation/content/multilingual.md index 443e7e49..cd697ff5 100644 --- a/docs/content/documentation/content/multilingual.md +++ b/docs/content/documentation/content/multilingual.md @@ -33,8 +33,8 @@ uses the filename to detect the language: - `content/an-article.md`: this will be the default language - `content/an-article.fr.md`: this will be in French -If the language code in the filename does not correspond to one of the languages configured, -an error will be shown. +If the language code in the filename does not correspond to one of the languages or +the default language configured, an error will be shown. If your default language has an `_index.md` in a directory, you will need to add an `_index.{code}.md` file with the desired front-matter options as there is no language fallback.