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

i18n #333

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

i18n #333

Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@
"comlink": "^4.3.0",
"d3": "^6.5.0",
"dayjs": "^1.8.33",
"i18next": "^21.3.3",
"i18next-browser-languagedetector": "^6.1.2",
"i18next-chained-backend": "^3.0.2",
"i18next-resources-to-backend": "^1.0.0",
"lit-html": "^1.3.0",
"lodash": "^4.17.15",
"rdf-literal": "^1.2.0",
"react": "^16.13.1",
"react": "^17.0.2",
"react-bootstrap": "^1.5.0",
"react-color": "2.17.3",
"react-cookie-consent": "^6.2.3",
"react-country-flag": "^3.0.1",
"react-data-grid": "^7.0.0-canary.16",
"react-dnd": "^11.1.3",
"react-dnd-html5-backend": "^11.1.3",
"react-dom": "^16.13.1",
"react-dom": "^17.0.2",
"react-dropzone": "^11.0.3",
"react-i18next": "^11.13.0",
"react-icons": "^3.10.0",
"react-scripts": "3.4.1",
"react-scripts": "^4.0.3",
"sparql-http-client": "^2.4.0",
"sparqljs": "^3.5.1",
"typeface-inter": "^3.12.0",
Expand Down
9 changes: 5 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import { serializeProject } from '@rawgraphs/rawgraphs-core'
import useDataLoader from './hooks/useDataLoader'
import isPlainObject from 'lodash/isPlainObject'
import CookieConsent from 'react-cookie-consent'

// #TODO: i18n
import { useTranslation } from 'react-i18next'

function App() {
const dataLoader = useDataLoader()
Expand Down Expand Up @@ -162,15 +161,17 @@ function App() {
setVisualOptions(getDefaultOptionsValues(options))
}, [])

const { t } = useTranslation()

return (
<div className="App">
<Header menuItems={HeaderItems} />
<div className="app-sections">
<Section title={`1. Load your data`} loading={loading}>
<Section title={`1. ${t('loadData')}`} loading={loading}>
<DataLoader {...dataLoader} hydrateFromProject={importProject} />
</Section>
{data && (
<Section title="2. Choose a chart">
<Section title={`2. ${t('chooseChart')}`}>
<ChartSelector
availableCharts={charts}
currentChart={currentChart}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import { Row, Col } from 'react-bootstrap'
import isObject from 'lodash/isObject'
import { useLazyTranslation } from '../../../hooks/useLazyTranslation'

const ChartOptionSelect = ({
options = [],
Expand All @@ -11,6 +12,7 @@ const ChartOptionSelect = ({
label,
...props
}) => {
const lazyt = useLazyTranslation()
return (
<Row className={props.className}>
<Col xs={6} className="d-flex align-items-center nowrap">{label}</Col>
Expand All @@ -28,7 +30,7 @@ const ChartOptionSelect = ({
{options.map((option) =>
isObject(option) ? (
<option key={option.value} value={option.value}>
{option.label}
{lazyt(option.label)}
</option>
) : (
<option key={option} value={option}>
Expand Down
7 changes: 5 additions & 2 deletions src/components/ChartOptions/ChartOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import get from 'lodash/get'
import map from 'lodash/map'
import styles from './ChartOptions.module.scss'
import omit from 'lodash/omit'
import { useLazyTranslation } from '../../hooks/useLazyTranslation'

const CHART_OPTION_COMPONENTS = {
number: ChartOptionNumber,
Expand Down Expand Up @@ -162,6 +163,8 @@ function WrapControlComponent({
[optionId, repeatIndex, setVisualOptions]
)

const lazyt = useLazyTranslation()

return (
<Component
type={type}
Expand All @@ -173,10 +176,10 @@ function WrapControlComponent({
label={
repeatIndex !== undefined ? (
<React.Fragment>
{label} ({repeatIndex + 1})
{lazyt(label)} ({repeatIndex + 1})
</React.Fragment>
) : (
label
lazyt(label)
)
}
{...omit(props, [
Expand Down
14 changes: 11 additions & 3 deletions src/components/ChartPreview/ChartPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { chart as rawChart } from '@rawgraphs/rawgraphs-core'
import useDebounce from '../../hooks/useDebounce'
import WarningMessage from '../WarningMessage'
import { onChartRendered } from '../../gaEvents'
import LazyTrans from '../LazyTrans'

const ChartPreview = ({
chart,
Expand Down Expand Up @@ -40,7 +41,11 @@ const ChartPreview = ({
<span>
Required chart variables: you need to map{' '}
{requiredVariables
.map((d, i) => <span key={i} className="font-weight-bold">{d.name}</span>)
.map((d, i) => (
<span key={i} className="font-weight-bold">
<LazyTrans>{d.name}</LazyTrans>
</span>
))
.reduce((prev, curr) => [prev, ' and ', curr])}
</span>
)
Expand Down Expand Up @@ -68,7 +73,10 @@ const ChartPreview = ({
.map((d) => (
<>
at least <span className="font-weight-bold">{d.minValues}</span>{' '}
dimensions on <span className="font-weight-bold">{d.name}</span>
dimensions on{' '}
<span className="font-weight-bold">
<LazyTrans>{d.name}</LazyTrans>
</span>
</>
))
.reduce((prev, curr) => [prev, ' and ', curr])}
Expand Down Expand Up @@ -123,7 +131,7 @@ const ChartPreview = ({
setError(null)
onChartRendered(chart.metadata)
} catch (e) {
console.log("chart error", e)
console.log('chart error', e)
setError({ variant: 'danger', message: 'Chart error. ' + e.message })
setRawViz(null)
}
Expand Down
21 changes: 13 additions & 8 deletions src/components/ChartSelector/ChartSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import classNames from 'classnames'
import { Row, Col, Card, Dropdown } from 'react-bootstrap'
import { BsLink } from 'react-icons/bs'
import uniq from 'lodash/uniq'
import capitalize from 'lodash/capitalize'
import styles from './ChartSelector.module.scss'
import { useLazyTranslation } from '../../hooks/useLazyTranslation'

function filterCharts(charts, filter) {
return filter === 'All charts'
Expand All @@ -28,6 +30,7 @@ function ChartSelector({ availableCharts, currentChart, setCurrentChart }) {
},
[availableCharts, currentChart, setCurrentChart]
)
const lazyt = useLazyTranslation()

return (
<>
Expand All @@ -36,7 +39,7 @@ function ChartSelector({ availableCharts, currentChart, setCurrentChart }) {
Show
<Dropdown className="d-inline-block ml-2 raw-dropdown">
<Dropdown.Toggle variant="white" className="pr-5">
{filter.charAt(0).toUpperCase() + filter.slice(1)}
{capitalize(lazyt(filter))}
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item
Expand All @@ -50,7 +53,7 @@ function ChartSelector({ availableCharts, currentChart, setCurrentChart }) {
).map((d) => {
return (
<Dropdown.Item key={d} onClick={() => handleFilterChange(d)}>
{d.charAt(0).toUpperCase() + d.slice(1)}
{capitalize(lazyt(d))}
</Dropdown.Item>
)
})}
Expand All @@ -70,7 +73,9 @@ function ChartSelector({ availableCharts, currentChart, setCurrentChart }) {
<Card.Subtitle className="m-0">
<h4 className="mb-2">{currentChart.metadata.category}</h4>
</Card.Subtitle>
<Card.Text>{currentChart.metadata.description}</Card.Text>
<Card.Text>
{lazyt(currentChart.metadata.description)}
</Card.Text>
<Card.Link
className={classNames({
[styles.disabled]: !currentChart.metadata.code,
Expand Down Expand Up @@ -120,11 +125,11 @@ function ChartSelector({ availableCharts, currentChart, setCurrentChart }) {
</Card.Title>
<Card.Subtitle className="m-0">
<h4 className="m-0">
{d.metadata.categories
.join(', ')
.charAt(0)
.toUpperCase() +
d.metadata.categories.join(', ').slice(1)}
{capitalize(
d.metadata.categories
.map((c) => lazyt(c))
.join(', ')
)}
</h4>
</Card.Subtitle>
</Card.Body>
Expand Down
5 changes: 4 additions & 1 deletion src/components/DataMapping/ChartDimensionCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import ChartDimensionItem from './ChartDimensionItem'

import styles from './DataMapping.module.scss'
import { useLazyTranslation } from '../../hooks/useLazyTranslation'
const aggregators = getAggregatorNames()
const emptyList = []

Expand Down Expand Up @@ -193,6 +194,8 @@ const ChartDimensionCard = ({
[dataTypes, dimension, mapping, setDraggingId, setMapping]
)

const lazyt = useLazyTranslation()

return (
// <div
// className="Xcard Xp-3 Xm-2 "
Expand All @@ -216,7 +219,7 @@ const ChartDimensionCard = ({
// )
})}
</span>
<span className="text-capitalize text-center">{dimension.name}</span>
<span className="text-capitalize text-center">{lazyt(dimension.name)}</span>
<span
className={styles['dimension-required'] + ' text-right'}
style={{ opacity: dimension.required ? 1 : 0 }}
Expand Down
63 changes: 61 additions & 2 deletions src/components/Header/Header.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,73 @@
import React from 'react'
import find from 'lodash/find'
import styles from './Header.module.scss'
import { Navbar, Nav } from 'react-bootstrap'
import { Navbar, Nav, NavDropdown } from 'react-bootstrap'
import { ReactCountryFlag } from 'react-country-flag'
import { useTranslation } from 'react-i18next'

const FLAGS = [
{
code: 'en',
country: 'gb',
label: 'English',
},
{
code: 'it',
country: 'it',
label: 'Italiano',
},
{
code: 'fr',
country: 'fr',
label: 'Français',
},
]

export default function Header({ menuItems }) {
const { i18n } = useTranslation()

return (
<Navbar bg="white" expand="lg" sticky="top" className={styles.navbar}>
<Navbar.Brand href="/"><b>RAW</b><span className="text-primary">Graphs</span> 2.0 beta</Navbar.Brand>
<Navbar.Brand href="/">
<b>RAW</b>
<span className="text-primary">Graphs</span> 2.0 beta
</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="ml-auto">
<NavDropdown
title={
<ReactCountryFlag
style={{
height: 15,
width: 15,
}}
svg
countryCode={
find(FLAGS, { code: i18n.language })?.country ?? 'gb'
}
/>
}
id="basic-nav-dropdown"
>
{FLAGS.map((f) => (
<NavDropdown.Item
active={i18n.language === f.code}
key={f.code}
onSelect={() => i18n.changeLanguage(f.code)}
>
<ReactCountryFlag
countryCode={f.country}
svg
style={{
height: 15,
width: 15,
}}
/>{' '}
{f.label}
</NavDropdown.Item>
))}
</NavDropdown>
{menuItems.map((d, i) => {
return (
<Nav.Link key={'item' + i} href={d.href}>
Expand Down
6 changes: 6 additions & 0 deletions src/components/LazyTrans/LazyTrans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useLazyTranslation } from '../../hooks/useLazyTranslation'

export default function LazyTrans({ children }) {
const lazyt = useLazyTranslation()
return lazyt(children)
}
1 change: 1 addition & 0 deletions src/components/LazyTrans/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './LazyTrans'
11 changes: 11 additions & 0 deletions src/hooks/useLazyTranslation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isRAWLazyT, valueOfRAWLazyT } from '@rawgraphs/rawgraphs-core'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'

export function useLazyTranslation(...args) {
const { t: baseT } = useTranslation(...args)
return useCallback(
(s) => (isRAWLazyT(s) ? baseT('RAWCharts.' + valueOfRAWLazyT(s)) : s),
[baseT]
)
}
37 changes: 37 additions & 0 deletions src/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import i18next from 'i18next'
import ChainedBackend from 'i18next-chained-backend'
import resourcesToBackend from 'i18next-resources-to-backend'
import LanguageDetector from 'i18next-browser-languagedetector'
import { initReactI18next } from 'react-i18next'

i18next
.use(LanguageDetector)
.use(ChainedBackend)
.use(initReactI18next)
.init({
supportedLngs: ['it', 'en', 'fr'],
preload: ['en'],
fallbackLng: 'en',
debug: process.env.NODE_ENV !== 'production',
detection: {
order: ['localStorage', 'navigator'],
caches: ['localStorage'],
},
backend: {
backends: [
resourcesToBackend((lng, ns, clb) => {
Promise.all([
import(`./locales/${lng}.json`).then((r) => r.default),
import(`@rawgraphs/rawgraphs-charts/locales/${lng}.json`).then(
(r) => r.default
),
])
.then((resources) => {
const r = { ...resources[0], RAWCharts: resources[1] }
clb(null, r)
})
.catch(clb)
}),
],
},
})
Loading