Skip to content

Instantly share code, notes, and snippets.

@apsrcreatix
Created December 24, 2024 10:05
Show Gist options
  • Save apsrcreatix/8f66556440971a726e78c1625188fd70 to your computer and use it in GitHub Desktop.
Save apsrcreatix/8f66556440971a726e78c1625188fd70 to your computer and use it in GitHub Desktop.
Lokalise - Helper/Code generator for react-intl based implementation
// path to english or your primary language file so it can generate helper code for you
import en from "./en.json";
// matches takes string input which try to find if that word exist in keys of the lokalise exported json you provided
function generateLokaliseHelperData(matches: string) {
try {
const translation = JSON.parse(JSON.stringify(en))
let results: any = "{"
let match = matches
for (const [key, value] of Object.entries(translation)) {
let _key = key
if (_key.search(match) !== -1) {
let splitted_key = _key.split(".")
results += `"${
splitted_key[splitted_key?.length - 1]
}": intl.formatMessage({id: "${key}",defaultMessage: "${String(
value,
)}"}),`
}
}
return results + "}"
} catch (error) {
console.error({ error })
return {}
}
}
export default generateLokaliseHelperData;
// console log the result it sent
// copy content from console and paste it in file against your variable
// example: let translation = "paste your content here"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment