You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

site_i18n.rs 6.0KB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. mod common;
  2. use std::env;
  3. use common::build_site;
  4. use site::Site;
  5. #[test]
  6. fn can_parse_multilingual_site() {
  7. let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
  8. path.push("test_site_i18n");
  9. let mut site = Site::new(&path, "config.toml").unwrap();
  10. site.load().unwrap();
  11. let library = site.library.read().unwrap();
  12. assert_eq!(library.pages().len(), 10);
  13. assert_eq!(library.sections().len(), 6);
  14. // default index sections
  15. let default_index_section =
  16. library.get_section(&path.join("content").join("_index.md")).unwrap();
  17. assert_eq!(default_index_section.pages.len(), 1);
  18. assert!(default_index_section.ancestors.is_empty());
  19. let fr_index_section = library.get_section(&path.join("content").join("_index.fr.md")).unwrap();
  20. assert_eq!(fr_index_section.pages.len(), 1);
  21. assert!(fr_index_section.ancestors.is_empty());
  22. // blog sections get only their own language pages
  23. let blog_path = path.join("content").join("blog");
  24. let default_blog = library.get_section(&blog_path.join("_index.md")).unwrap();
  25. assert_eq!(default_blog.subsections.len(), 0);
  26. assert_eq!(default_blog.pages.len(), 4);
  27. assert_eq!(
  28. default_blog.ancestors,
  29. vec![*library.get_section_key(&default_index_section.file.path).unwrap()]
  30. );
  31. for key in &default_blog.pages {
  32. let page = library.get_page_by_key(*key);
  33. assert_eq!(page.lang, "en");
  34. }
  35. let fr_blog = library.get_section(&blog_path.join("_index.fr.md")).unwrap();
  36. assert_eq!(fr_blog.subsections.len(), 0);
  37. assert_eq!(fr_blog.pages.len(), 3);
  38. assert_eq!(
  39. fr_blog.ancestors,
  40. vec![*library.get_section_key(&fr_index_section.file.path).unwrap()]
  41. );
  42. for key in &fr_blog.pages {
  43. let page = library.get_page_by_key(*key);
  44. assert_eq!(page.lang, "fr");
  45. }
  46. }
  47. #[test]
  48. fn can_build_multilingual_site() {
  49. let (_, _tmp_dir, public) = build_site("test_site_i18n");
  50. assert!(public.exists());
  51. // Index pages
  52. assert!(file_exists!(public, "index.html"));
  53. assert!(file_exists!(public, "fr/index.html"));
  54. assert!(file_contains!(public, "fr/index.html", "Une page"));
  55. assert!(file_contains!(public, "fr/index.html", "Language: fr"));
  56. assert!(file_exists!(public, "base/index.html"));
  57. assert!(file_exists!(public, "fr/base/index.html"));
  58. // Sections are there as well, with translations info
  59. assert!(file_exists!(public, "blog/index.html"));
  60. assert!(file_contains!(
  61. public,
  62. "blog/index.html",
  63. "Translated in fr: Mon blog https://example.com/fr/blog/"
  64. ));
  65. assert!(file_contains!(
  66. public,
  67. "blog/index.html",
  68. "Translated in it: Il mio blog https://example.com/it/blog/"
  69. ));
  70. assert!(file_exists!(public, "fr/blog/index.html"));
  71. assert!(file_contains!(public, "fr/blog/index.html", "Language: fr"));
  72. assert!(file_contains!(
  73. public,
  74. "fr/blog/index.html",
  75. "Translated in en: My blog https://example.com/blog/"
  76. ));
  77. assert!(file_contains!(
  78. public,
  79. "fr/blog/index.html",
  80. "Translated in it: Il mio blog https://example.com/it/blog/"
  81. ));
  82. // Normal pages are there with the translations
  83. assert!(file_exists!(public, "blog/something/index.html"));
  84. assert!(file_contains!(
  85. public,
  86. "blog/something/index.html",
  87. "Translated in fr: Quelque chose https://example.com/fr/blog/something/"
  88. ));
  89. assert!(file_exists!(public, "fr/blog/something/index.html"));
  90. assert!(file_contains!(public, "fr/blog/something/index.html", "Language: fr"));
  91. assert!(file_contains!(
  92. public,
  93. "fr/blog/something/index.html",
  94. "Translated in en: Something https://example.com/blog/something/"
  95. ));
  96. // sitemap contains all languages
  97. assert!(file_exists!(public, "sitemap.xml"));
  98. assert!(file_contains!(public, "sitemap.xml", "https://example.com/blog/something-else/"));
  99. assert!(file_contains!(public, "sitemap.xml", "https://example.com/fr/blog/something-else/"));
  100. assert!(file_contains!(public, "sitemap.xml", "https://example.com/it/blog/something-else/"));
  101. // one rss per language
  102. assert!(file_exists!(public, "rss.xml"));
  103. assert!(file_contains!(public, "rss.xml", "https://example.com/blog/something-else/"));
  104. assert!(!file_contains!(public, "rss.xml", "https://example.com/fr/blog/something-else/"));
  105. assert!(file_exists!(public, "fr/rss.xml"));
  106. assert!(!file_contains!(public, "fr/rss.xml", "https://example.com/blog/something-else/"));
  107. assert!(file_contains!(public, "fr/rss.xml", "https://example.com/fr/blog/something-else/"));
  108. // Italian doesn't have RSS enabled
  109. assert!(!file_exists!(public, "it/rss.xml"));
  110. // Taxonomies are per-language
  111. // English
  112. assert!(file_exists!(public, "authors/index.html"));
  113. assert!(file_contains!(public, "authors/index.html", "Queen"));
  114. assert!(!file_contains!(public, "authors/index.html", "Vincent"));
  115. assert!(!file_exists!(public, "auteurs/index.html"));
  116. assert!(file_exists!(public, "authors/queen-elizabeth/rss.xml"));
  117. assert!(file_exists!(public, "tags/index.html"));
  118. assert!(file_contains!(public, "tags/index.html", "hello"));
  119. assert!(!file_contains!(public, "tags/index.html", "bonjour"));
  120. // French
  121. assert!(!file_exists!(public, "fr/authors/index.html"));
  122. assert!(file_exists!(public, "fr/auteurs/index.html"));
  123. assert!(!file_contains!(public, "fr/auteurs/index.html", "Queen"));
  124. assert!(file_contains!(public, "fr/auteurs/index.html", "Vincent"));
  125. assert!(!file_exists!(public, "fr/auteurs/vincent-prouillet/rss.xml"));
  126. assert!(file_exists!(public, "fr/tags/index.html"));
  127. assert!(file_contains!(public, "fr/tags/index.html", "bonjour"));
  128. assert!(!file_contains!(public, "fr/tags/index.html", "hello"));
  129. // one lang index per language
  130. assert!(file_exists!(public, "search_index.en.js"));
  131. assert!(file_exists!(public, "search_index.it.js"));
  132. assert!(!file_exists!(public, "search_index.fr.js"));
  133. }