Skip to content

Commit

Permalink
feat: Add "useI18n" hook
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Apr 1, 2021
1 parent 2596711 commit 20cb17a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
12 changes: 6 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"parser": "babel-eslint",
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false
},

"root": true,
"extends": ["@asd14/eslint-config/targets/react"],
"ignorePatterns": ["dist/*.js"],
Expand All @@ -22,10 +26,6 @@
// Recommended if you use eslint_d
"import/cache": {
"lifetime": 5
},

// Can add a path segment here that will act like a source root, for
// in-project aliasing (i.e. `import MyStore from 'stores/my-store'`)
"import/resolver": "webpack"
}
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export { usePrevious } from "./use-previous"
export { useQuery } from "./use-query"
export { useResize } from "./use-resize"
export { Portal } from "./use-portal"
export { useI18n } from "./use-i18n/i18n.hook"
26 changes: 26 additions & 0 deletions src/use-i18n/i18n.hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const debug = require("debug")("@asd14/react-hooks:useI18n")

import { useMemo } from "react"
import { split, map, pipe, read } from "@asd14/m"
import { useRouteMatch, useLocation } from "react-router-dom"

export const useI18n = () => {
const match = useRouteMatch()
const location = useLocation()

debug({ match, location, asd: process.env.LANGUAGES })

return {
languages: useMemo(
() =>
pipe(
split(","),
map([split(":"), ([id, label]) => ({ id, label })])
)(process.env.LANGUAGES),
[]
),

language: read(["params", "language"], process.env.LANGUAGE_DEFAULT, match),
defaultLanguage: process.env.LANGUAGE_DEFAULT ?? "en",
}
}
26 changes: 26 additions & 0 deletions src/use-i18n/i18n.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const debug = require("debug")("@asd14/react-hooks:I18n.Component")

import PropTypes from "prop-types"
import { split } from "@asd14/m"

import { useEffect } from "../use-deep"

const I18n = ({ languages, defaultLanguage }) => {
useEffect(() => {
}, [defaultLanguage, languages])

/* eslint-disable unicorn/no-null */
return null
}

I18n.propTypes = {
languages: PropTypes.string,
defaultLanguage: PropTypes.string,
}

I18n.defaultProps = {
languages: ["en"],
defaultLanguage: "en",
}

export { I18n }
30 changes: 30 additions & 0 deletions src/use-i18n/i18n.redux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const debug = require("debug")("@asd14/react-hooks:useI18nRedux")

const defaultState = {
languages: localStorage.getItem(STORE_KEY) ?? [
{ id: "en", label: "English" },
{ id: "nl", label: "Dutch" },
{ id: "ro", label: "Română" },
],
defaultLanguage:
localStorage.getItem(`${STORE_KEY}_DEFAULT`) ??
process.env.LANGUAGE_DEFAULT,
selected:
localStorage.getItem(`${STORE_KEY}_SELECTED`) ??
process.env.LANGUAGE_DEFAULT,
}

export const STORE_KEY = "GLOBAL.I18N"

export const reducer = (state = defaultState, { type, payload = {} }) => {
switch (type) {
case `${STORE_KEY}.SET`:
return {
...state,
...payload,
}

default:
return state
}
}

0 comments on commit 20cb17a

Please sign in to comment.