@qonsoll/translation

0.5.9 • Public • Published

Firebase RealTime Database Translation

props:

  • children,
  • defaultApp
  • defaultLanguage = 'en'
  • onWrite
  • onRead
  • storage = window?.localStorage
  • languages = [{ name: 'English', shortCode: 'en' }]
  • initialState = {}
  • useHashes = false
  • saveNewAutomatically = false
  • isLive = false

Example:

const TranslationProvider = (props) => {
  const { children, currentApp = DEFAULT_APP } = props

  // [HELPER_FUNCTIONS]
  const onWrite = useCallback(
    ({ ref, value }) => database().ref(ref).update(value),
    []
  )
  const onRead = useCallback(
    ({ ref: path, setTranslations, options }) =>
      database()
        .ref(path)
        .on(
          'value',
          (snapshot) => setTranslations(snapshot?.val() || {}),
          options
        ),
    []
  )

  return (
    <Provider
      storage={AsyncStorage}
      db={database()}
      onWrite={onWrite}
      useHashes
      onRead={onRead}
      saveNewAutomatically
      languages={LANGUAGES}
      defaultLanguage={DEFAULT_LANGUAGE}
      currentApp={currentApp}>
      <TranslationContext.Consumer>
        {({ loaded }) =>
          loaded ? children : <ScreenLoading text="Translation is loading" />
        }
      </TranslationContext.Consumer>
    </Provider>
  )
}

Context value:

  • setCurrentLanguage
  • language
  • translations
  • saveTranslationForLanguage = ({ textLabel, shortCode, refEnding, appName }) => Promise
  • loading
  • loaded
  • languages
  • translationsRDBRef: translations/${currentApp}/${language}
  • t

Readme

Keywords

none

Package Sidebar

Install

npm i @qonsoll/translation

Weekly Downloads

3

Version

0.5.9

License

none

Unpacked Size

53.3 kB

Total Files

4

Last publish

Collaborators

  • anhelina_bohdanova
  • swordsonline
  • alexfry123
  • vladbogach
  • slava.shamrelyuk
  • aleksandrgolyk
  • olenashevchuk
  • artfun00
  • sashka2131
  • rttsu
  • alexpassss
  • evgeniy.bogdanov
  • shudya_