From e85f9342de61ce30664a8ea2781a9b93ef8417cf Mon Sep 17 00:00:00 2001 From: Samuel Plumppu <6125097+Greenheart@users.noreply.github.com> Date: Tue, 23 Apr 2024 17:41:54 +0200 Subject: [PATCH] Feature/basic i18n (#452) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Init i18next * Remove unused file * Switch to next-i18next * Add configuration and translate cookie banner * Fix Next.js warning "API handler should not return a value, received object" * Translate footer and add eslint rule to avoid import mistake * Ensure SSR and CSR bundles provide consistent i18n config. See https://github.com/i18next/next-i18next/issues/2259 for details * Remove unused page * Remove unused property * Add top-level translations for StartPage * No need for translations when redirecting * Add 404 translations * Simplify DropDown to remove unused classes * Fix formatting error in Header * Remove Footer tests since they 1) it wasn't worth adapting them to the new i18n structure, and 2) because they tested the content rather than cricital functionality * Combine footer and common translations into a single namespace * Fix formatting issue * Add basic i18n to remaining pages * Use markdown to simplify i18n for dataset definitions * Style Markdown content according to the theme, with the option to override components * Use markdown for `body` field of DataDescriptions * Simplify date formatting * Prevent styled-components from rendering invalid HTML props * Remove unused components * Add translations for common components * Fix layout issue * Begin adding municipality translations * Add translations for municipality numbers * Allow ScorecardSections to use consistent formattig even with markdown content * Add municipality ScoreCard translations * Use HTML details element to make ScorecardSection more accessible and easier to use * Styling tweaks * Add missing key attribute * Fix React hydration error caused by missing element * Add solutions translations * Improve styles for Markdown * Replace JSX with Markdown formatting * Use markdown formatting instead of JS override * Translate utsläppsberakningar and use Markdown for simplified formatting * Translate partierna, and update next/image props * Translate Källor och Metod * Improve grid styling on about page * Translate about page * Improve spacing * Clarify dataset task ideas * Translate sitemap * Update tests to use mocked i18n and test against expected keys rather than values * Begin translating datasets * Translate the easy parts of dataset definitions * Clarify naming * Replace deprecated method String#substr with String#slice * Refactor to prepare for translated dataDescriptions * Add translations for datasets * WIP: hack around broken SSR translations * Cleanup SSR translations * Separate hardcoded dataset keys from the UI labels, to allow translations * Update tests to reflect fully translated datasets * Reduce margin-top for FactSection * Fix translation error * Fit images in container width * Fix modal positioning. Also improve UX by closing on backdrop click. Long term our custom Modal should be replaced with a standard Modal solution. * Ensure special data for climate plans and procurements renders correctly. Also improve TypeScript types to prevent this error in the future. * Fix type error * Revert spacing change for CementClarification --- .eslintrc.js | 6 + __tests__/components/Footer/Footer.test.tsx | 57 - .../Footer/FooterNewsletterForm.test.tsx | 38 - .../Footer/FooterNewsletterSubscribe.test.tsx | 22 - .../components/Footer/FooterPartners.test.tsx | 31 - .../Footer/FooterSocialLinks.test.tsx | 37 - __tests__/index.test.tsx | 19 +- .../utils/generateMunipacitySitemap.test.ts | 6 +- components/ComparisonTable.tsx | 12 +- components/DropDown.tsx | 12 +- components/FactSection.tsx | 9 +- components/Footer/Footer.tsx | 19 +- components/Footer/FooterNewsletterForm.tsx | 11 +- components/Footer/FooterSocialLinks.tsx | 6 +- components/Graph.tsx | 6 +- components/Header.tsx | 22 +- components/InfoModal.tsx | 19 +- components/Layout.tsx | 2 +- components/Map/MapLabels.tsx | 4 +- components/Markdown.tsx | 43 + components/Municipality/Municipality.tsx | 13 +- .../MunicipalityEmissionGraph.tsx | 106 +- .../MunicipalityEmissionNumbers.tsx | 12 +- .../Municipality/MunicipalityScorecard.tsx | 130 +- .../Municipality/MunicipalitySolutions.tsx | 66 +- components/Municipality/ScorecardSection.tsx | 27 +- components/Navigation.tsx | 69 - components/RadioButtonMenu.tsx | 27 +- components/Range.tsx | 34 - components/ToggleSection.tsx | 5 +- components/shared.tsx | 12 +- next-i18next.config.js | 14 + next.config.js | 9 +- package-lock.json | 2378 +++++++++++------ package.json | 7 + pages/404.tsx | 15 +- pages/[dataset]/[dataView]/index.tsx | 11 +- pages/[dataset]/index.tsx | 10 +- pages/_app.tsx | 25 +- pages/api/map.tsx | 2 - pages/in-english/index.tsx | 15 +- pages/index.tsx | 55 +- pages/kallor-och-metod/index.tsx | 249 +- pages/kommun/[municipality]/[step].tsx | 5 +- pages/om-oss/index.tsx | 418 +-- pages/partierna/index.tsx | 136 +- pages/sitemap.xml.ts | 14 +- pages/utslappsberakningar/index.tsx | 163 +- public/locales/sv/about.json | 77 + public/locales/sv/common.json | 123 + public/locales/sv/kallorMetod.json | 23 + public/locales/sv/municipality.json | 101 + public/locales/sv/partierna.json | 9 + public/locales/sv/sitemap.json | 4 + public/locales/sv/startPage.json | 15 + public/locales/sv/utslappsberakningar.json | 12 + public/vercel.svg | 4 - utils/createMunicipalityList.tsx | 44 +- utils/datasetDefinitions.tsx | 461 ++-- utils/generateMunipacitySitemap.ts | 9 +- utils/getServerSideI18n.ts | 26 + utils/shared.ts | 17 +- utils/types.ts | 21 +- 63 files changed, 2696 insertions(+), 2658 deletions(-) delete mode 100644 __tests__/components/Footer/Footer.test.tsx delete mode 100644 __tests__/components/Footer/FooterNewsletterForm.test.tsx delete mode 100644 __tests__/components/Footer/FooterNewsletterSubscribe.test.tsx delete mode 100644 __tests__/components/Footer/FooterPartners.test.tsx delete mode 100644 __tests__/components/Footer/FooterSocialLinks.test.tsx create mode 100644 components/Markdown.tsx delete mode 100644 components/Navigation.tsx delete mode 100644 components/Range.tsx create mode 100644 next-i18next.config.js create mode 100644 public/locales/sv/about.json create mode 100644 public/locales/sv/common.json create mode 100644 public/locales/sv/kallorMetod.json create mode 100644 public/locales/sv/municipality.json create mode 100644 public/locales/sv/partierna.json create mode 100644 public/locales/sv/sitemap.json create mode 100644 public/locales/sv/startPage.json create mode 100644 public/locales/sv/utslappsberakningar.json delete mode 100644 public/vercel.svg create mode 100644 utils/getServerSideI18n.ts diff --git a/.eslintrc.js b/.eslintrc.js index 2fb8cda9..ce37830e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -32,5 +32,11 @@ module.exports = { 'import/no-extraneous-dependencies': 'off', // fixme revisit and refactor files when there's time to spare 'react/react-in-jsx-scope': 'off', 'react/jsx-one-expression-per-line': 'warn', + 'no-restricted-imports': ['error', { + paths: [{ + name: 'react-i18next', + message: 'Please import useTranslation from next-i18next.', + }], + }], }, } diff --git a/__tests__/components/Footer/Footer.test.tsx b/__tests__/components/Footer/Footer.test.tsx deleted file mode 100644 index ab4ff964..00000000 --- a/__tests__/components/Footer/Footer.test.tsx +++ /dev/null @@ -1,57 +0,0 @@ -import { render, screen } from '@testing-library/react' -import Footer from '../../../components/Footer/Footer' - -describe('Footer component', () => { - it('should render without crashing', () => { - render( -