feat(fs.lab): translation with props
This commit is contained in:
parent
490f48f726
commit
825ca14189
3 changed files with 19 additions and 13 deletions
|
@ -1,5 +1,6 @@
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import set from 'lodash.set'
|
import set from 'lodash.set'
|
||||||
|
import mustache from 'mustache'
|
||||||
// Stores state in local storage
|
// Stores state in local storage
|
||||||
import useLocalStorage from 'shared/hooks/useLocalStorage.js'
|
import useLocalStorage from 'shared/hooks/useLocalStorage.js'
|
||||||
// Patterns
|
// Patterns
|
||||||
|
@ -90,19 +91,23 @@ function useApp(full = true) {
|
||||||
*/
|
*/
|
||||||
const t = (key, props=false, toLanguage=false) => {
|
const t = (key, props=false, toLanguage=false) => {
|
||||||
if (!toLanguage) toLanguage = language
|
if (!toLanguage) toLanguage = language
|
||||||
if (!props) { // easy
|
const template =
|
||||||
if (strings[toLanguage][key]) return strings[toLanguage][key]
|
strings[toLanguage][key] ||
|
||||||
// app is the most common prefix, so we allow to skip it
|
strings[toLanguage][`app.${key}`] ||
|
||||||
if (strings[toLanguage][`app.${key}`]) return strings[toLanguage][`app.${key}`]
|
strings.en[`app.${key}`] ||
|
||||||
// Can we fall back to English?
|
false
|
||||||
if (toLanguage !== 'en') {
|
if (!props && template) return template
|
||||||
if (strings.en[key]) return strings.en[key]
|
else if (template) return mustache.render(
|
||||||
if (strings.en[`app.${key}`]) return strings.en[`app.${key}`]
|
template.split('{').join('{{').split('}').join('}}'),
|
||||||
}
|
props
|
||||||
}
|
)
|
||||||
console.log('Missing translation key:', key)
|
// No translation found
|
||||||
|
//console.log('Missing translation key:', key)
|
||||||
|
|
||||||
return key
|
// If it's a key (no spaces), return the final part, that's slightly better
|
||||||
|
return (key.indexOf(' ') === -1)
|
||||||
|
? key.split('.').pop()
|
||||||
|
: key
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
"lodash.get": "^4.4.2",
|
"lodash.get": "^4.4.2",
|
||||||
"lodash.orderby": "^4.6.0",
|
"lodash.orderby": "^4.6.0",
|
||||||
"lodash.set": "^4.3.2",
|
"lodash.set": "^4.3.2",
|
||||||
|
"mustache": "^4.2.0",
|
||||||
"netlify-cli": "^8.4.2",
|
"netlify-cli": "^8.4.2",
|
||||||
"next": "latest",
|
"next": "latest",
|
||||||
"react-copy-to-clipboard": "^5.0.4",
|
"react-copy-to-clipboard": "^5.0.4",
|
||||||
|
|
|
@ -19557,7 +19557,7 @@ multiparty@^4.2.1:
|
||||||
safe-buffer "5.2.1"
|
safe-buffer "5.2.1"
|
||||||
uid-safe "2.1.5"
|
uid-safe "2.1.5"
|
||||||
|
|
||||||
mustache@^4.0.1:
|
mustache@^4.0.1, mustache@^4.2.0:
|
||||||
version "4.2.0"
|
version "4.2.0"
|
||||||
resolved "https://registry.yarnpkg.com/mustache/-/mustache-4.2.0.tgz#e5892324d60a12ec9c2a73359edca52972bf6f64"
|
resolved "https://registry.yarnpkg.com/mustache/-/mustache-4.2.0.tgz#e5892324d60a12ec9c2a73359edca52972bf6f64"
|
||||||
integrity sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==
|
integrity sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue