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 set from 'lodash.set'
|
||||
import mustache from 'mustache'
|
||||
// Stores state in local storage
|
||||
import useLocalStorage from 'shared/hooks/useLocalStorage.js'
|
||||
// Patterns
|
||||
|
@ -90,19 +91,23 @@ function useApp(full = true) {
|
|||
*/
|
||||
const t = (key, props=false, toLanguage=false) => {
|
||||
if (!toLanguage) toLanguage = language
|
||||
if (!props) { // easy
|
||||
if (strings[toLanguage][key]) return strings[toLanguage][key]
|
||||
// app is the most common prefix, so we allow to skip it
|
||||
if (strings[toLanguage][`app.${key}`]) return strings[toLanguage][`app.${key}`]
|
||||
// Can we fall back to English?
|
||||
if (toLanguage !== 'en') {
|
||||
if (strings.en[key]) return strings.en[key]
|
||||
if (strings.en[`app.${key}`]) return strings.en[`app.${key}`]
|
||||
}
|
||||
}
|
||||
console.log('Missing translation key:', key)
|
||||
const template =
|
||||
strings[toLanguage][key] ||
|
||||
strings[toLanguage][`app.${key}`] ||
|
||||
strings.en[`app.${key}`] ||
|
||||
false
|
||||
if (!props && template) return template
|
||||
else if (template) return mustache.render(
|
||||
template.split('{').join('{{').split('}').join('}}'),
|
||||
props
|
||||
)
|
||||
// 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 {
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
"lodash.get": "^4.4.2",
|
||||
"lodash.orderby": "^4.6.0",
|
||||
"lodash.set": "^4.3.2",
|
||||
"mustache": "^4.2.0",
|
||||
"netlify-cli": "^8.4.2",
|
||||
"next": "latest",
|
||||
"react-copy-to-clipboard": "^5.0.4",
|
||||
|
|
|
@ -19557,7 +19557,7 @@ multiparty@^4.2.1:
|
|||
safe-buffer "5.2.1"
|
||||
uid-safe "2.1.5"
|
||||
|
||||
mustache@^4.0.1:
|
||||
mustache@^4.0.1, mustache@^4.2.0:
|
||||
version "4.2.0"
|
||||
resolved "https://registry.yarnpkg.com/mustache/-/mustache-4.2.0.tgz#e5892324d60a12ec9c2a73359edca52972bf6f64"
|
||||
integrity sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue