Skip to main content

generate_locale_redirect

Function generate_locale_redirect 

Source
pub fn generate_locale_redirect(
    site_dir: &Path,
    available_locales: &[String],
    default_locale: &str,
) -> Result<(), SsgError>
Expand description

Generates a root index.html that reads the browser’s language preference and redirects to the best matching locale directory.

The file is written at site_dir/index.html. If it already exists and was not generated by this function, it is left untouched.

§Examples

use ssg::server::generate_locale_redirect;
use tempfile::tempdir;

let dir = tempdir().unwrap();
generate_locale_redirect(dir.path(), &["en".into(), "fr".into()], "en").unwrap();
let html = std::fs::read_to_string(dir.path().join("index.html")).unwrap();
assert!(html.contains("ssg-locale-redirect"));

§Errors

Returns an error if the file cannot be written.