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 5.8KB

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