Skip to content

Commit

Permalink
add lang hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Herkarl committed Jun 30, 2024
1 parent 1004d45 commit b55a4b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Fragment } from 'react'
import { Routes, Route, Link, useSearchParams, useLocation } from 'react-router-dom'
import React from 'react'
import { Routes, Route, Link, useLocation } from 'react-router-dom'

import Taitan from './Taitan'
import Methone from 'methone'
Expand All @@ -12,7 +12,7 @@ import { comparePages } from '../utility/compare'
import { LanguageContext } from './Translate'

import './App.css'
import { addLangToUrl } from '../utility/lang'
import { addLangToUrl, useLang } from '../utility/lang'

const createLinks = (nav, lang) => nav
.sort(comparePages)
Expand Down Expand Up @@ -53,14 +53,13 @@ const renderMethone = (path, lang) => {
}

export const App = () => {
const [searchParams,] = useSearchParams()
const lang = searchParams.get("lang")
const lang = useLang()
const location = useLocation()

return (
<div id="application" className="cerise">
{renderMethone(location.pathname, lang)}
<LanguageContext.Provider value={lang}>
<LanguageContext.Provider value={lang || 'sv'}>
<Routes>
<Route path="/" exact element={<Frontpage lang={lang} />} />
<Route path="/nyheter/:postId" element={<SingleNews lang={lang} />} />
Expand Down
7 changes: 7 additions & 0 deletions src/utility/lang.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { useSearchParams } from "react-router-dom"

// handles the case if the lang is undefinede
export const addLangToUrl = (url, lang) => {
return lang ? url + "?lang=" + lang : url
}

// fetches value of the "lang" get param. Can be null
export const useLang = () => {
const [searchParams, ] = useSearchParams()
return searchParams.get("lang")
}

0 comments on commit b55a4b2

Please sign in to comment.