8000 GitHub - qonsoll/translation
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

qonsoll/translation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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(
    async ({ ref: path, setTranslations, options }) => {
      const write = (snapshot) => setTranslations(snapshot?.val() || {})

      const ref = database().ref(path)
      if (options?.onlyOnce) {
        return ref.once('value', write)
      } else {
        return ref.on('value', write)
      }
    },
    []
  )

  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 = (string) => void
  • setCurrentApp = ({ appName, withLoading }) => void
  • currentApp
  • language
  • translations
  • saveTranslationForLanguage = ({ textLabel, shortCode, refEnding, appName }) => Promise
  • loading
  • loaded
  • languages
  • translationsRDBRef: translations/${currentApp}/${language}
  • t = (string) => string

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  
0