-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b610f54
commit 95018d2
Showing
3 changed files
with
57 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,40 @@ | ||
import React, {useContext} from 'react' | ||
import { gql } from '@apollo/client'; | ||
import React, { useContext } from "react"; | ||
import { gql } from "@apollo/client"; | ||
|
||
const TranslationContext = React.createContext([]); | ||
|
||
const getTranslationsData = async (client, lang) => { | ||
const { data } = await client.query({ | ||
variables: {lang}, | ||
query: gql` | ||
query TranslationQuery($lang: String) { | ||
texts { | ||
text_id | ||
translations(filter: {languages_code: {url_code: {_eq: $lang}}}) { | ||
text | ||
} | ||
} | ||
} | ||
` | ||
}); | ||
|
||
return data.texts.reduce((acc, it) => { | ||
acc[it.text_id] = it.translations[0].text | ||
return acc | ||
}, {}) | ||
} | ||
const { data } = await client.query({ | ||
variables: { lang }, | ||
query: gql` | ||
query TranslationQuery($lang: String) { | ||
texts { | ||
text_id | ||
translations( | ||
filter: { languages_code: { url_code: { _eq: $lang } } } | ||
) { | ||
text | ||
} | ||
} | ||
} | ||
`, | ||
}); | ||
|
||
const TranslatedField = ({tKey}) => { | ||
const translations = useContext(TranslationContext); | ||
const item = translations[tKey]; | ||
if(!item) throw "Missing translation with key " + tKey; | ||
return item; | ||
} | ||
return data.texts.reduce((acc, it) => { | ||
acc[it.text_id] = it.translations[0].text; | ||
return acc; | ||
}, {}); | ||
}; | ||
|
||
const TranslatedField = ({ tKey }) => { | ||
const translations = useContext(TranslationContext); | ||
const item = translations[tKey]; | ||
|
||
if (!item) { | ||
console.error("Missing translation for key " + tKey); | ||
} | ||
return item; | ||
}; | ||
|
||
export default TranslatedField; | ||
export { getTranslationsData, TranslationContext }; | ||
export { getTranslationsData, TranslationContext }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters