nav.js 892 B

12345678910111213141516171819202122232425
  1. import { computed } from 'vue';
  2. import { useData, useRoute } from 'vitepress';
  3. export function useLanguageLinks() {
  4. const { site, localePath, theme } = useData();
  5. return computed(() => {
  6. const langs = site.value.langs;
  7. const localePaths = Object.keys(langs);
  8. // one language
  9. if (localePaths.length < 2) {
  10. return null;
  11. }
  12. const route = useRoute();
  13. // intentionally remove the leading slash because each locale has one
  14. const currentPath = route.path.replace(localePath.value, '');
  15. const candidates = localePaths.map((localePath) => ({
  16. text: langs[localePath].label,
  17. link: `${localePath}${currentPath}`
  18. }));
  19. const selectText = theme.value.selectText || 'Languages';
  20. return {
  21. text: selectText,
  22. items: candidates
  23. };
  24. });
  25. }