Guest User

Untitled

a guest
Feb 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. // load your translations
  2. // add whatever number namespaces you need
  3. const localesNSContent = {
  4. 'en':[
  5. {
  6. content:fs.readFileSync(`src/locales/en/translations.json`, 'utf8'),
  7. ns:'translations'
  8. }
  9. ],
  10. 'pt':[
  11. {
  12. content:fs.readFileSync(`src/locales/pt/translations.json`, 'utf8'),
  13. ns:'translations'
  14. }
  15. ],
  16. };
  17.  
  18. const availableLocales = [
  19. { value: 'en', text: 'English' },
  20. { value: 'de', text: 'Deutsch' }
  21. ]
  22.  
  23. exports.onCreatePage = async (props) => {
  24. const { page, actions: { createPage, deletePage, createRedirect } } = props;
  25.  
  26. deletePage(page);
  27. if (page.path.includes('404')) {
  28. createPage({
  29. ...page,
  30. context: {
  31. locale: 'en-us',
  32. data: localesNSContent['en-us']
  33. }
  34. })
  35. }
  36.  
  37. if (page.path === '/') {
  38. createPage({
  39. ...page,
  40. context: {
  41. availableLocales
  42. }
  43. });
  44. } else {
  45. availableLocales.map(({ value }) => {
  46. let newPath = `/${value}${page.path}`;
  47. if (page.path === '/homepage/') {
  48. newPath = `/${value}`
  49. }
  50. const localePage = {
  51. ...page,
  52. originalPath: page.path,
  53. path: newPath,
  54. context: {
  55. availableLocales,
  56. locale: value,
  57. routed: true,
  58. data: localesNSContent[value],
  59. originalPath: page.path
  60. },
  61. }
  62. createPage(localePage)
  63. });
  64. }
  65. }
Add Comment
Please, Sign In to add comment