From 779511ae43f2e23bf17c98090e79e7b45b1f1d61 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 28 Dec 2018 13:24:49 +0100 Subject: [PATCH] Pass down lang and start docs --- components/library/src/content/page.rs | 1 + components/library/src/content/section.rs | 1 + components/library/src/content/ser.rs | 6 ++++ .../documentation/content/multilingual.md | 34 +++++++++++++++++++ .../getting-started/configuration.md | 11 +++++- .../documentation/templates/overview.md | 1 + .../documentation/templates/pages-sections.md | 4 +++ test_site_i18n/templates/index.html | 1 + 8 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 docs/content/documentation/content/multilingual.md diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index 870ef6bb..dea402d4 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -252,6 +252,7 @@ impl Page { context.insert("current_url", &self.permalink); context.insert("current_path", &self.path); context.insert("page", &self.to_serialized(library)); + context.insert("lang", &self.lang); render_template(&tpl_name, tera, &context, &config.theme) .chain_err(|| format!("Failed to render page '{}'", self.file.path.display())) diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index c8369eb9..c711bbca 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -184,6 +184,7 @@ impl Section { context.insert("current_url", &self.permalink); context.insert("current_path", &self.path); context.insert("section", &self.to_serialized(library)); + context.insert("lang", &self.lang); render_template(tpl_name, tera, &context, &config.theme) .chain_err(|| format!("Failed to render section '{}'", self.file.path.display())) diff --git a/components/library/src/content/ser.rs b/components/library/src/content/ser.rs index 6fbe51b2..1c902eb6 100644 --- a/components/library/src/content/ser.rs +++ b/components/library/src/content/ser.rs @@ -30,6 +30,7 @@ pub struct SerializingPage<'a> { toc: &'a [Header], assets: &'a [String], draft: bool, + lang: &'a Option, lighter: Option>>, heavier: Option>>, earlier: Option>>, @@ -88,6 +89,7 @@ impl<'a> SerializingPage<'a> { toc: &page.toc, assets: &page.serialized_assets, draft: page.is_draft(), + lang: &page.lang, lighter, heavier, earlier, @@ -136,6 +138,7 @@ impl<'a> SerializingPage<'a> { toc: &page.toc, assets: &page.serialized_assets, draft: page.is_draft(), + lang: &page.lang, lighter: None, heavier: None, earlier: None, @@ -157,6 +160,7 @@ pub struct SerializingSection<'a> { components: &'a [String], word_count: Option, reading_time: Option, + lang: &'a Option, toc: &'a [Header], assets: &'a [String], pages: Vec>, @@ -196,6 +200,7 @@ impl<'a> SerializingSection<'a> { reading_time: section.reading_time, toc: §ion.toc, assets: §ion.serialized_assets, + lang: §ion.lang, pages, subsections, } @@ -227,6 +232,7 @@ impl<'a> SerializingSection<'a> { reading_time: section.reading_time, toc: §ion.toc, assets: §ion.serialized_assets, + lang: §ion.lang, pages: vec![], subsections: vec![], } diff --git a/docs/content/documentation/content/multilingual.md b/docs/content/documentation/content/multilingual.md new file mode 100644 index 00000000..5dd1aaad --- /dev/null +++ b/docs/content/documentation/content/multilingual.md @@ -0,0 +1,34 @@ ++++ +title = "Multilingual sites" +weight = 130 ++++ + +Zola supports having a site in multiple languages. + +## Configuration +To get started, you will need to add the languages you want to support +to your `config.toml`. For example: + +```toml +languages = [ + {code = "fr", rss = true}, # there will be a RSS feed for French content + {code = "it"}, # there won't be a RSS feed for Italian content +] +``` + +## Content +Once the languages are added in, you can start to translate your content. Zola +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 your default language has an `_index.md` in a directory, you will need to add a `_index.{code}.md` +file with the desired front-matter options as there is no language fallback. + +## Output +Zola outputs the translated content with a base URL of `{base_url}/{code}/`. +The only exception to that is if you are setting a translated page `path` directly in the front-matter. diff --git a/docs/content/documentation/getting-started/configuration.md b/docs/content/documentation/getting-started/configuration.md index 97e633e0..2acba61f 100644 --- a/docs/content/documentation/getting-started/configuration.md +++ b/docs/content/documentation/getting-started/configuration.md @@ -21,7 +21,7 @@ base_url = "mywebsite.com" # Used in RSS by default title = "" description = "" -# the default language, used in RSS and coming i18n +# The default language, used in RSS default_language = "en" # Theme name to use @@ -51,6 +51,15 @@ generate_rss = false # taxonomies = [] +# The additional languages for that site +# Example: +# languages = [ +# {code = "fr", rss = true}, # there will be a RSS feed for French content +# {code = "it"}, # there won't be a RSS feed for Italian content +# ] +# +languages = [] + # Whether to compile the Sass files found in the `sass` directory compile_sass = false diff --git a/docs/content/documentation/templates/overview.md b/docs/content/documentation/templates/overview.md index 0ada7282..bf9aad72 100644 --- a/docs/content/documentation/templates/overview.md +++ b/docs/content/documentation/templates/overview.md @@ -18,6 +18,7 @@ A few variables are available on all templates minus RSS and sitemap: - `config`: the [configuration](./documentation/getting-started/configuration.md) without any modifications - `current_path`: the path (full URL without the `base_url`) of the current page, never starting with a `/` - `current_url`: the full URL for that page +- `lang`: the language for that page, `null` if the page/section doesn't have a language set ## Standard Templates By default, Zola will look for three templates: `index.html`, which is applied diff --git a/docs/content/documentation/templates/pages-sections.md b/docs/content/documentation/templates/pages-sections.md index e12ae718..88476a23 100644 --- a/docs/content/documentation/templates/pages-sections.md +++ b/docs/content/documentation/templates/pages-sections.md @@ -51,6 +51,8 @@ assets: Array; ancestors: Array; // The relative path from the `content` directory to the markdown file relative_path: String; +// The language for the page if there is one +lang: String? ``` ## Section variables @@ -93,6 +95,8 @@ assets: Array; ancestors: Array; // The relative path from the `content` directory to the markdown file relative_path: String; +// The language for the section if there is one +lang: String? ``` ## Table of contents diff --git a/test_site_i18n/templates/index.html b/test_site_i18n/templates/index.html index f2e96a59..e1b47f13 100644 --- a/test_site_i18n/templates/index.html +++ b/test_site_i18n/templates/index.html @@ -1,3 +1,4 @@ {% for page in section.pages %} {{page.title}} {% endfor %} +Language: {{lang}}