Skip to content

Commit

Permalink
refactor(I18n): Extract useExtendI18n into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Sep 25, 2024
1 parent be077bb commit b58109d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
3 changes: 2 additions & 1 deletion react/providers/I18n/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ I18n.childContextTypes = {
lang: PropTypes.string
}

export { initTranslation, extend, useExtendI18n } from './translation'
export { initTranslation, extend } from './translation'
export { default as translate } from './translate'
export { default as createUseI18n } from './createUseI18n'
export { default as useExtendI18n } from './useExtendI18n'

export default I18n
33 changes: 1 addition & 32 deletions react/providers/I18n/translation.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Polyglot from 'node-polyglot'

import { DEFAULT_LANG, useI18n } from '.'
import { DEFAULT_LANG } from '.'

export let _polyglot

Expand Down Expand Up @@ -40,34 +40,3 @@ export const initTranslation = (
}

export const extend = (dict, polyglot) => (polyglot || _polyglot)?.extend(dict)

// Use to determine if we need to merge locales again, and to avoid useless calls
let useExtendI18nLang = ''

/**
* Hook to merge app locales with cozy-ui locales
* @param {object} locales - Locales sorted by lang `{ fr: {...}, en: {...} }`
* @returns {void}
*/
export const useExtendI18n = locales => {
const { lang, polyglot } = useI18n()

if (!locales || !lang || !polyglot) return

// To simplify code we use Polyglot.extend to merge
// locales from object and from polyglot.phrases
// rather than native JS or lodash. this is why we have two extend.
if (useExtendI18nLang !== lang) {
const _polyglot = new Polyglot({
phrases: locales[lang],
locale: lang
})

// merge locales from app and cozy-ui, without replacing existing one in app
extend(polyglot.phrases, _polyglot)
// use merged locales in app
extend(_polyglot.phrases, polyglot)
// set the sitch to avoid useless merge
useExtendI18nLang = lang
}
}
37 changes: 37 additions & 0 deletions react/providers/I18n/useExtendI18n.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Polyglot from 'node-polyglot'

import { useI18n } from '.'
import { extend } from './translation'

// Use to determine if we need to merge locales again, and to avoid useless calls
let useExtendI18nLang = ''

/**
* Hook to merge app locales with cozy-ui locales
* @param {object} locales - Locales sorted by lang `{ fr: {...}, en: {...} }`
* @returns {void}
*/
const useExtendI18n = locales => {
const { lang, polyglot } = useI18n()

if (!locales || !lang || !polyglot) return

// To simplify code we use Polyglot.extend to merge
// locales from object and from polyglot.phrases
// rather than native JS or lodash. this is why we have two extend.
if (useExtendI18nLang !== lang) {
const _polyglot = new Polyglot({
phrases: locales[lang],
locale: lang
})

// merge locales from app and cozy-ui, without replacing existing one in app
extend(polyglot.phrases, _polyglot)
// use merged locales in app
extend(_polyglot.phrases, polyglot)
// set the sitch to avoid useless merge
useExtendI18nLang = lang
}
}

export default useExtendI18n

0 comments on commit b58109d

Please sign in to comment.