Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/basic i18n #452

Merged
merged 65 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
bed81b0
Init i18next
Greenheart Apr 2, 2024
30d23e8
Remove unused file
Greenheart Apr 8, 2024
e7518f2
Switch to next-i18next
Greenheart Apr 8, 2024
ea4757d
Add configuration and translate cookie banner
Greenheart Apr 8, 2024
21cafdb
Fix Next.js warning "API handler should not return a value, received …
Greenheart Apr 8, 2024
bca465c
Translate footer and add eslint rule to avoid import mistake
Greenheart Apr 8, 2024
2aa4caa
Ensure SSR and CSR bundles provide consistent i18n config. See https:…
Greenheart Apr 8, 2024
d3b13e2
Remove unused page
Greenheart Apr 8, 2024
593020a
Remove unused property
Greenheart Apr 8, 2024
d6d25b8
Add top-level translations for StartPage
Greenheart Apr 8, 2024
31565f9
No need for translations when redirecting
Greenheart Apr 8, 2024
a99a542
Add 404 translations
Greenheart Apr 8, 2024
b9e84df
Simplify DropDown to remove unused classes
Greenheart Apr 8, 2024
4cebc41
Fix formatting error in Header
Greenheart Apr 8, 2024
cbafb07
Remove Footer tests since they 1) it wasn't worth adapting them to th…
Greenheart Apr 9, 2024
7bbeb20
Combine footer and common translations into a single namespace
Greenheart Apr 10, 2024
1905d7f
Fix formatting issue
Greenheart Apr 10, 2024
fb639c0
Add basic i18n to remaining pages
Greenheart Apr 10, 2024
259cff7
Use markdown to simplify i18n for dataset definitions
Greenheart Apr 12, 2024
3bd3605
Style Markdown content according to the theme, with the option to ove…
Greenheart Apr 12, 2024
6e5c4bc
Use markdown for `body` field of DataDescriptions
Greenheart Apr 12, 2024
fd7d1eb
Simplify date formatting
Greenheart Apr 12, 2024
a3b67d3
Prevent styled-components from rendering invalid HTML props
Greenheart Apr 12, 2024
0e6d49e
Remove unused components
Greenheart Apr 12, 2024
286b5d9
Add translations for common components
Greenheart Apr 12, 2024
3bc50bf
Fix layout issue
Greenheart Apr 12, 2024
a9ddbb2
Begin adding municipality translations
Greenheart Apr 12, 2024
82c7723
Add translations for municipality numbers
Greenheart Apr 12, 2024
c13b892
Allow ScorecardSections to use consistent formattig even with markdow…
Greenheart Apr 13, 2024
d213d49
Add municipality ScoreCard translations
Greenheart Apr 13, 2024
bf3df3f
Use HTML details element to make ScorecardSection more accessible and…
Greenheart Apr 13, 2024
03b4e56
Styling tweaks
Greenheart Apr 13, 2024
7f85f41
Add missing key attribute
Greenheart Apr 13, 2024
b2f9202
Fix React hydration error caused by missing <thead> element
Greenheart Apr 13, 2024
14737f2
Add solutions translations
Greenheart Apr 13, 2024
16e5c6b
Improve styles for Markdown
Greenheart Apr 13, 2024
ec84cb9
Replace JSX with Markdown formatting
Greenheart Apr 13, 2024
ea39c2b
Use markdown formatting instead of JS override
Greenheart Apr 13, 2024
2ec1cd5
Translate utsläppsberakningar and use Markdown for simplified formatting
Greenheart Apr 13, 2024
7b7e2d9
Translate partierna, and update next/image props
Greenheart Apr 14, 2024
fc1d5d3
Translate Källor och Metod
Greenheart Apr 14, 2024
e16656b
Improve grid styling on about page
Greenheart Apr 14, 2024
85b25c3
Translate about page
Greenheart Apr 14, 2024
a210460
Improve spacing
Greenheart Apr 14, 2024
e9e84c3
Clarify dataset task ideas
Greenheart Apr 15, 2024
19a1a0c
Translate sitemap
Greenheart Apr 16, 2024
9ecf96c
Update tests to use mocked i18n and test against expected keys rather…
Greenheart Apr 16, 2024
b550046
Begin translating datasets
Greenheart Apr 16, 2024
12cd12a
Translate the easy parts of dataset definitions
Greenheart Apr 16, 2024
3481d32
Clarify naming
Greenheart Apr 17, 2024
5fce3c4
Replace deprecated method String#substr with String#slice
Greenheart Apr 17, 2024
dfff371
Refactor to prepare for translated dataDescriptions
Greenheart Apr 17, 2024
66a6d64
Add translations for datasets
Greenheart Apr 17, 2024
b78312c
WIP: hack around broken SSR translations
Greenheart Apr 17, 2024
3326701
Cleanup SSR translations
Greenheart Apr 17, 2024
a11d5e8
Separate hardcoded dataset keys from the UI labels, to allow translat…
Greenheart Apr 17, 2024
f483739
Merge branch 'staging' into feature/basic-i18n
Greenheart Apr 17, 2024
3bf295b
Update tests to reflect fully translated datasets
Greenheart Apr 17, 2024
a5d2c7d
Reduce margin-top for FactSection
Greenheart Apr 18, 2024
b5d4097
Fix translation error
Greenheart Apr 18, 2024
7614db2
Fit images in container width
Greenheart Apr 18, 2024
aa49784
Fix modal positioning. Also improve UX by closing on backdrop click. …
Greenheart Apr 18, 2024
7d209ee
Ensure special data for climate plans and procurements renders correc…
Greenheart Apr 18, 2024
adea2bf
Fix type error
Greenheart Apr 18, 2024
c1d0dbe
Revert spacing change for CementClarification
Greenheart Apr 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
}],
}],
},
}
57 changes: 0 additions & 57 deletions __tests__/components/Footer/Footer.test.tsx

This file was deleted.

38 changes: 0 additions & 38 deletions __tests__/components/Footer/FooterNewsletterForm.test.tsx

This file was deleted.

22 changes: 0 additions & 22 deletions __tests__/components/Footer/FooterNewsletterSubscribe.test.tsx

This file was deleted.

31 changes: 0 additions & 31 deletions __tests__/components/Footer/FooterPartners.test.tsx

This file was deleted.

37 changes: 0 additions & 37 deletions __tests__/components/Footer/FooterSocialLinks.test.tsx

This file was deleted.

19 changes: 13 additions & 6 deletions __tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { fireEvent, render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import Home from '../pages/index'

vi.mock('../public/icons/info.svg', () => ({ default: () => 'svg' }))
Expand All @@ -22,6 +21,12 @@ vi.mock('next/router', () => ({
}),
}))

vi.mock('next-i18next', () => ({
useTranslation: vi.fn(() => ({
t: (str: string) => str,
})),
}))

describe('Home Page', () => {
// Mock data for municipalities
const mockMunicipalities = [
Expand Down Expand Up @@ -65,6 +70,8 @@ describe('Home Page', () => {
TotalConsumptionEmission: 0,
ElectricVehiclePerChargePoints: 0,
Name: 'Sollentuna',
ProcurementScore: 0,
ProcurementLink: '',
},
]

Expand All @@ -73,24 +80,24 @@ describe('Home Page', () => {
})

it('renders without crashing', () => {
expect(screen.getByText(/Hur går det med?/)).toBeInTheDocument()
expect(screen.getByText(/startPage:questionTitle/)).toBeInTheDocument()
})

it('changes view mode when toggle button is clicked', () => {
const toggleButton = screen.getByText('Listvy')
const toggleButton = screen.getByText('startPage:toggleView.list')
fireEvent.click(toggleButton)
expect(screen.getByText('Kartvy')).toBeInTheDocument()
expect(screen.getByText('startPage:toggleView.map')).toBeInTheDocument()
})

it('handles dataset change', () => {
const newDataset = 'Klimatplanerna'
const newDataset = 'common:datasets.plans.name'
const radioButton = screen.getByLabelText(newDataset)
fireEvent.click(radioButton)
expect(screen.getByText(newDataset)).toBeInTheDocument()
})

it('renders the dropdown component', () => {
const dropdownInput = screen.getByPlaceholderText(/hur går det i din kommun?/i)
const dropdownInput = screen.getByPlaceholderText(/startPage:yourMunicipality/i)
expect(dropdownInput).toBeInTheDocument()
})
})
6 changes: 5 additions & 1 deletion __tests__/utils/generateMunipacitySitemap.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { TFunction } from 'next-i18next'
import {
generateMunipacitySitemapData,
generateSitemap,
} from '../../utils/generateMunipacitySitemap'
import { Municipality } from '../../utils/types'

const municipalities = [{ Name: 'Stockholm' }, { Name: 'Göteborg' }] as Municipality[]

const t = vi.fn((str) => str) as unknown as TFunction

describe('generateSitemap', () => {
it('should generate valid municipality sitemap data', () => {
const siteMap = generateMunipacitySitemapData({ municipalities })
Expand All @@ -28,7 +32,7 @@ describe('generateSitemap', () => {

it('should generate a valid sitemap XML string', () => {
const siteMap = generateMunipacitySitemapData({ municipalities })
const sitemapXml = generateSitemap(siteMap)
const sitemapXml = generateSitemap(siteMap, t)
expect(() => new DOMParser().parseFromString(sitemapXml, 'text/xml')).not.toThrow()
})
})
12 changes: 7 additions & 5 deletions components/ComparisonTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ function ComparisonTable<T extends object>({ data, columns }: TableProps<T>) {

return (
<StyledTable key={resizeCount}>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header, index) => renderHeader(header, index))}
</tr>
))}
<thead>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header, index) => renderHeader(header, index))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.map((row) => (
<TableRow key={row.id} onClick={() => handleRowClick(row)}>
Expand Down
12 changes: 7 additions & 5 deletions components/DropDown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useState, useRef, useEffect } from 'react'
import styled from 'styled-components'
import { useRouter } from 'next/router'
import { useTranslation } from 'next-i18next'

import ArrowDown from '../public/icons/arrow-down.svg'
import { devices } from '../utils/devices'

Expand Down Expand Up @@ -83,7 +85,6 @@ const ErrorText = styled.div`
type Props = {
municipalitiesName: Array<string>
placeholder: string
className: string
}

export function getSortedMunicipalities(municipalitiesName: Array<string>) {
Expand Down Expand Up @@ -113,7 +114,7 @@ export function search(query: string, municipalitiesName: Array<string>) {
})
}

function DropDown({ municipalitiesName, placeholder, className }: Props) {
function DropDown({ municipalitiesName, placeholder }: Props) {
const sortedMunicipalities = getSortedMunicipalities(municipalitiesName)
const [showDropDown, setShowDropDown] = useState(false)
const [selectedMunicipality, setSelectedMunicipality] = useState<string>('')
Expand All @@ -122,6 +123,7 @@ function DropDown({ municipalitiesName, placeholder, className }: Props) {

const ref = useRef<HTMLDivElement>(null)
const router = useRouter()
const { t } = useTranslation()

useEffect(() => {
const checkIfClickedOutside = (e: MouseEvent) => {
Expand Down Expand Up @@ -192,13 +194,13 @@ function DropDown({ municipalitiesName, placeholder, className }: Props) {
value={selectedMunicipality}
/>
<Btn onClick={() => setShowDropDown((current) => !current)}>
<ArrowDown aria-label="Visa kommun" />
<ArrowDown aria-label={t('common:components.DropDown.label')} />
</Btn>
</Flex>
{showDropDown && (
<MunicipalitiesWrapper className={className}>
<MunicipalitiesWrapper>
{municipalities.map((name) => (
<Municipality onClick={() => onMunicipalityClick(name)}>
<Municipality onClick={() => onMunicipalityClick(name)} key={name}>
{name}
</Municipality>
))}
Expand Down
9 changes: 7 additions & 2 deletions components/FactSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useState } from 'react'
import { H3, ParagraphBold } from './Typography'
import Icon from '../public/icons/add_light_white.svg'
import IconGreen from '../public/icons/remove_light_white.svg'
import Markdown from './Markdown'

const Row = styled.summary`
display: flex;
Expand Down Expand Up @@ -40,6 +41,10 @@ const InfoSection = styled.div`
text-decoration: underline;
cursor: pointer;
}

& p:first-of-type {
margin-top: 0;
}
`

const StyledIcon = styled.div`
Expand All @@ -53,7 +58,7 @@ const StyledIcon = styled.div`
type Props = {
heading: string
data: string
info?: JSX.Element | string
info?: string
}

function FactSection({ heading, data, info }: Props) {
Expand All @@ -75,7 +80,7 @@ function FactSection({ heading, data, info }: Props) {
)}
</Row>
<InfoSection>
{info}
<Markdown>{info}</Markdown>
</InfoSection>
</details>
)
Expand Down
Loading