Skip to content

Commit

Permalink
2661 - Add Lang type to i18nFactory (#4037)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaguzmang authored Oct 17, 2024
1 parent 787c2c4 commit abb9978
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/client/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { initReactI18next } from 'react-i18next'
import { createParams } from 'i18n/i18nFactory'
import i18n from 'i18next'

import { Lang } from 'meta/lang'

i18n
.use(initReactI18next) // passes i18n down to react-i18next
.init(createParams('en'))
.init(createParams(Lang.en))

export default i18n
46 changes: 23 additions & 23 deletions src/i18n/i18nFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,50 @@ import { zhTranslation } from './resources/zh'
// @ts-ignore
const createInstance = i18next.createInstance || i18next.default.createInstance

const translationsFiles: { [langCode: string]: any } = {
en: enTranslation,
es: esTranslation,
fr: frTranslation,
ru: ruTranslation,
ar: arTranslation,
zh: zhTranslation,
const translationsFiles = {
[Lang.en]: enTranslation,
[Lang.es]: esTranslation,
[Lang.fr]: frTranslation,
[Lang.ru]: ruTranslation,
[Lang.ar]: arTranslation,
[Lang.zh]: zhTranslation,
}

export const createParams = (lang: string) => ({
fallbackLng: 'en',
export const createParams = (lang: Lang) => ({
fallbackLng: Lang.en,
debug: false,

// react i18next special options (optional)
react: {
wait: false, // set to true if you like to wait for loaded in every translated hoc
nsMode: 'default', // set it to fallback to let passed namespaces to translated hoc act as fallbacks
nsMode: 'default' as 'default' | 'fallback', // set it to fallback to let passed namespaces to translated hoc act as fallbacks
},

lng: lang,

resources: {
en: {
translation: translationsFiles.en,
[Lang.en]: {
translation: translationsFiles[Lang.en],
},
es: {
translation: translationsFiles.es,
[Lang.es]: {
translation: translationsFiles[Lang.es],
},
fr: {
translation: translationsFiles.fr,
[Lang.fr]: {
translation: translationsFiles[Lang.fr],
},
ru: {
translation: translationsFiles.ru,
[Lang.ru]: {
translation: translationsFiles[Lang.ru],
},
ar: {
translation: translationsFiles.ar,
[Lang.ar]: {
translation: translationsFiles[Lang.ar],
},
zh: {
translation: translationsFiles.zh,
[Lang.zh]: {
translation: translationsFiles[Lang.zh],
},
},
})

export const createI18nPromise = (lang: any): Promise<{ language: Lang; t: TFunction }> =>
export const createI18nPromise = (lang: Lang): Promise<{ language: Lang; t: TFunction }> =>
new Promise((resolve, reject) =>
// @ts-ignore
// eslint-disable-next-line no-promise-executor-return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CountryIso } from 'meta/area'
import { Assessment, Cycle } from 'meta/assessment'
import { ODPDataSourceMethod } from 'meta/assessment/originalDataPoint'
import { RecordAssessmentDatas } from 'meta/data'
import { Lang } from 'meta/lang'

import { OriginalDataPointRepository } from 'server/repository/assessmentCycle/originalDataPoint'

Expand Down Expand Up @@ -52,7 +53,7 @@ const getValue = async (props: {

export const getNDPYear = async (props: Props) => {
const { assessment, cycle, countries } = props
const { t } = await createI18nPromise('en')
const { t } = await createI18nPromise(Lang.en)

const climaticData = RecordAssessmentDatas.getCycleData({
assessmentName: assessment.props.name,
Expand Down
3 changes: 2 additions & 1 deletion src/server/controller/cycleData/getBulkDownload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { i18n as i18nType } from 'i18next'

import { Areas, CountryIso, RegionCode, RegionGroupName } from 'meta/area'
import { Assessment, Cycle } from 'meta/assessment'
import { Lang } from 'meta/lang'

import { getContent } from 'server/controller/cycleData/getBulkDownload/getContent'
import { getContentVariables } from 'server/controller/cycleData/getBulkDownload/getContentVariables'
Expand Down Expand Up @@ -81,7 +82,7 @@ const handleResult = ({ regions, iso3, name, year, ...row }: Record<string, stri
}

const handleContent = async (content: Array<Record<string, string>>) => {
const i18n = (await createI18nPromise('en')) as i18nType
const i18n = (await createI18nPromise(Lang.en)) as i18nType
// sort content by country iso and then by year
content.sort((a, b) => {
if (a.iso3 < b.iso3) return -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { i18n as i18nType } from 'i18next'
import { Objects } from 'utils/objects'

import { Assessment, Cycle, Labels } from 'meta/assessment'
import { Lang } from 'meta/lang'

import { TableRedisRepository } from 'server/repository/redis/table'

Expand Down Expand Up @@ -52,7 +53,7 @@ export const getMetadata = async (
const table = await TableRedisRepository.getOne(props)
const { rows } = table

const i18n = (await createI18nPromise('en')) as i18nType
const i18n = (await createI18nPromise(Lang.en)) as i18nType
const dateOfExport = `(${i18n.t('bulkDownload.dateOfExport')})`
const dateExported = `${new Date().toLocaleDateString('en-GB', {
day: '2-digit',
Expand Down
3 changes: 2 additions & 1 deletion src/server/service/mail/assessmentNotifyUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Arrays } from 'utils/arrays'

import { AssessmentStatus, Country, CountryIso } from 'meta/area'
import { AssessmentName, Cycle } from 'meta/assessment'
import { Lang } from 'meta/lang'
import { Routes } from 'meta/routes'
import { RoleName, User, Users } from 'meta/user'
import { UserRoles } from 'meta/user/userRoles'
Expand All @@ -25,7 +26,7 @@ type CreateMailProps = {
const createMail = async (props: CreateMailProps) => {
const { assessmentName, countryIso, cycleName, user, url, status, recipient, message } = props

const i18n = await createI18nPromise(recipient.props.lang ?? 'en')
const i18n = await createI18nPromise(recipient.props.lang ?? Lang.en)

const serverUrl = `${url}${Routes.Country.generatePath({ assessmentName, countryIso, cycleName })}`

Expand Down
3 changes: 2 additions & 1 deletion src/server/service/mail/oneToOneMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createI18nPromise } from 'i18n/i18nFactory'

import { CountryIso } from 'meta/area'
import { AssessmentName, Assessments } from 'meta/assessment'
import { Lang } from 'meta/lang'
import { Routes } from 'meta/routes'
import { User, Users } from 'meta/user'

Expand All @@ -17,7 +18,7 @@ export const oneToOneMessage = async (props: {
}) => {
const { assessmentName, countryIso, cycleName, recipient, sender, url } = props

const i18n = await createI18nPromise('en')
const i18n = await createI18nPromise(Lang.en)

const link = `${url}${Routes.CountryHome.generatePath({
assessmentName,
Expand Down
3 changes: 2 additions & 1 deletion src/server/service/mail/resetPassword.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createI18nPromise } from 'i18n/i18nFactory'

import { AssessmentName } from 'meta/assessment'
import { Lang } from 'meta/lang'
import { Routes } from 'meta/routes'
import { User, UserResetPassword, Users } from 'meta/user'

Expand All @@ -15,7 +16,7 @@ export const resetPassword = async (props: {
}) => {
const { assessmentName, cycleName, url, user, userResetPassword } = props

const i18n = await createI18nPromise('en')
const i18n = await createI18nPromise(Lang.en)

const link = `${url}${Routes.LoginResetPassword.generatePath(
{ assessmentName, cycleName },
Expand Down

0 comments on commit abb9978

Please sign in to comment.