# All files / src/_hooks I18n.tsx

100% Statements 40/40
66.66% Branches 2/3
100% Functions 4/4
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    11084x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x
    84x

    84x

    37x
    37x

    84x
    37x
    37x

    37x
    37x
    37x

    84x

    84x
    28x
    26x

    26x

    84x
    2761x

    2761x

    2761x

     import { createI18njs, I18njs, I18njsConfig } from '@procore/core-i18n-js'
    import * as R from 'ramda'
    import React from 'react'
    import deDE from '../_locales/de-DE.json'
    import enAU from '../_locales/en-AU.json'
    import enCA from '../_locales/en-CA.json'
    import enGB from '../_locales/en-GB.json'
    import enUS from '../_locales/en-US.json'
    import en from '../_locales/en.json'
    import esES from '../_locales/es-ES.json'
    import es from '../_locales/es.json'
    import frCA from '../_locales/fr-CA.json'
    import frFR from '../_locales/fr-FR.json'
    import isIS from '../_locales/is-IS.json'
    import isIT from '../_locales/it-IT.json'
    import jaJP from '../_locales/ja-JP.json'
    import ko from '../_locales/ko.json'
    import ptBR from '../_locales/pt-BR.json'
    import thTH from '../_locales/th-TH.json'
    import zhSG from '../_locales/zh-SG.json'

    type I18nConfig = Omit<I18njsConfig, 'I18n'>

    export type Locale =
      | 'en'
      | 'en-CA'
      | 'fr-CA'
      | 'fr-FR'
      | 'es'
      | 'es-ES'
      | 'en-AE'
      | 'en-AU'
      | 'en-GB'
      | 'en-SG'
      | 'en-US'
      | 'ko'
      | 'th-TH'
      | 'pt'
      | 'is-IS'
      | 'de-DE'
      | 'zh-SG'
      | 'pseudo'

    /* Set up i18n-js */
    export const coreTranslations = {
      'de-DE': deDE,
      en,
      'en-AU': enAU,
      'en-CA': enCA,
      'en-GB': enGB,
      'en-US': enUS,
      es,
      'es-ES': esES,
      'fr-CA': frCA,
      'fr-FR': frFR,
      'is-IS': isIS,
      'it-IT': isIT,
      'ja-JP': jaJP,
      ko,
      'pt-BR': ptBR,
      'th-TH': thTH,
      'zh-SG': zhSG,
    } as const

    function getValidLocale({ locale, translations }: I18njsConfig) {
      return locale
    }

    export function useI18n({
      locale: _locale,
      translations: _translations = {},
    }: I18njsConfig): I18njs {
      const translations = R.mergeDeepLeft(_translations, coreTranslations)
      const locale = getValidLocale({ locale: _locale, translations })
      return createI18njs({
        locale,
        translations,
      })
    }

    export const I18nContext: React.Context<I18njs> = React.createContext(
      createI18njs({
        locale: 'en',
        translations: coreTranslations, // inject our translations
      })
    )

    export function I18nProvider({
      children,
      ...props
    }: I18nConfig & { children: React.ReactNode }) {
      return (
        <I18nContext.Provider value={useI18n(props)}>
          {children}
        </I18nContext.Provider>
      )
    }

    export function useI18nContext() {
      const ctx = React.useContext(I18nContext)

      Iif (!ctx) {
        // TODO investigate a way to do warnings that won't annoy people
        // we probably need to introduce a build system to have nice dev warnings
        // console.warn()
      }

      return ctx
    }