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
This commit is contained in:
Basix 2020-11-11 19:12:24 +09:00 committed by Vincent Prouillet
parent cb75334498
commit 944cba030e
2 changed files with 20 additions and 2 deletions

View file

@ -129,6 +129,11 @@ impl FileInfo {
// We can document that
let mut parts: Vec<String> = 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();

View file

@ -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.