language-selector.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Language\LanguageCodes;
  4. use Grav\Common\Page\Page;
  5. use \Grav\Common\Plugin;
  6. class LanguageSelectorPlugin extends Plugin
  7. {
  8. /**
  9. * @return array
  10. */
  11. public static function getSubscribedEvents()
  12. {
  13. return [
  14. 'onPluginsInitialized' => ['onPluginsInitialized', 0]
  15. ];
  16. }
  17. /**
  18. * Initialize configuration
  19. */
  20. public function onPluginsInitialized()
  21. {
  22. if ($this->isAdmin()) {
  23. $this->active = false;
  24. return;
  25. }
  26. $this->enable([
  27. 'onTwigInitialized' => ['onTwigInitialized', 0],
  28. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  29. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
  30. ]);
  31. }
  32. /** Add the native_name function */
  33. public function onTwigInitialized()
  34. {
  35. $this->grav['twig']->twig()->addFunction(
  36. new \Twig_SimpleFunction('native_name', function($key) {
  37. return LanguageCodes::getNativeName($key);
  38. })
  39. );
  40. }
  41. /**
  42. * Add current directory to twig lookup paths.
  43. */
  44. public function onTwigTemplatePaths()
  45. {
  46. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  47. }
  48. /**
  49. * Set needed variables to display Language-Selector.
  50. */
  51. public function onTwigSiteVariables()
  52. {
  53. $data = new \stdClass;
  54. // Manage Data content
  55. $page = $this->grav['page'];
  56. $data->page_route = $page->rawRoute();
  57. if ($page->home()) {
  58. $data->page_route = '/';
  59. }
  60. $languages = $this->grav['language']->getLanguages();
  61. $data->languages = $languages;
  62. if ($this->config->get('plugins.language-selector.untranslated_pages_behavior') !== 'none') {
  63. $translated_pages = [];
  64. foreach ($languages as $language) {
  65. $translated_pages[$language] = null;
  66. $page_name_without_ext = substr($page->name(), 0, -(strlen($page->extension())));
  67. $translated_page_path = $page->path() . DS . $page_name_without_ext . '.' . $language . '.md';
  68. if (file_exists($translated_page_path)) {
  69. $translated_page = new Page();
  70. $translated_page->init(new \SplFileInfo($translated_page_path), $language . '.md');
  71. $translated_pages[$language] = $translated_page;
  72. }
  73. }
  74. $data->translated_pages = $translated_pages;
  75. }
  76. $data->current = $this->grav['language']->getLanguage();
  77. // Manage Twig Variables
  78. $path_flags = $this->grav['locator']->findResource('plugin://language-selector/flags/', false) . "/";
  79. $path_flags = $this->grav['base_url'] ."/". ltrim($path_flags, '/');
  80. $this->grav['twig']->twig_vars['language_selector'] = $data;
  81. $this->grav['twig']->twig_vars['language_display'] = array(
  82. "select" => $this->config->get('plugins.language-selector.select_display'),
  83. "button" => $this->config->get('plugins.language-selector.button_display'),
  84. );
  85. $this->grav['twig']->twig_vars['path_flags'] = $path_flags;
  86. // Manage Assets
  87. $this->grav['assets']->add('plugin://language-selector/js/language-selector.js');
  88. if ($this->config->get('plugins.language-selector.built_in_css')) {
  89. $this->grav['assets']->add('plugin://language-selector/css/language-selector.css');
  90. }
  91. }
  92. public function getNativeName($code) {
  93. }
  94. }