|
| 1 | +import _ from "lodash"; |
| 2 | +import { ILoader } from "./_types"; |
| 3 | +import { createLoader } from "./_utils"; |
| 4 | + |
| 5 | +export default function createJsonDictionaryLoader(): ILoader< |
| 6 | + Record<string, any>, |
| 7 | + Record<string, any> |
| 8 | +> { |
| 9 | + return createLoader({ |
| 10 | + pull: async (locale, input) => { |
| 11 | + return extractTranslatables(input, locale); |
| 12 | + }, |
| 13 | + push: async (locale, data, originalInput, originalLocale) => { |
| 14 | + if (!originalInput) { |
| 15 | + throw new Error("Error while parsing json-dictionary bucket"); |
| 16 | + } |
| 17 | + const input = _.cloneDeep(originalInput); |
| 18 | + |
| 19 | + // set the translation under the target locale key, use value from data (which is now a string) |
| 20 | + function walk(obj: any, dataNode: any, path: string[] = []) { |
| 21 | + if (Array.isArray(obj) && Array.isArray(dataNode)) { |
| 22 | + obj.forEach((item, idx) => |
| 23 | + walk(item, dataNode[idx], [...path, String(idx)]), |
| 24 | + ); |
| 25 | + } else if ( |
| 26 | + obj && |
| 27 | + typeof obj === "object" && |
| 28 | + dataNode && |
| 29 | + typeof dataNode === "object" && |
| 30 | + !Array.isArray(dataNode) |
| 31 | + ) { |
| 32 | + for (const key of Object.keys(obj)) { |
| 33 | + if (dataNode.hasOwnProperty(key)) { |
| 34 | + walk(obj[key], dataNode[key], [...path, key]); |
| 35 | + } |
| 36 | + } |
| 37 | + } else if ( |
| 38 | + obj && |
| 39 | + typeof obj === "object" && |
| 40 | + !Array.isArray(obj) && |
| 41 | + typeof dataNode === "string" |
| 42 | + ) { |
| 43 | + // dataNode is the new string for the target locale |
| 44 | + setNestedLocale(input, path, locale, dataNode, originalLocale); |
| 45 | + } |
| 46 | + } |
| 47 | + walk(originalInput, data); |
| 48 | + |
| 49 | + return input; |
| 50 | + }, |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +// extract all keys that match locale from object |
| 55 | +function extractTranslatables(obj: any, locale: string): any { |
| 56 | + if (Array.isArray(obj)) { |
| 57 | + return obj.map((item) => extractTranslatables(item, locale)); |
| 58 | + } else if (isTranslatableObject(obj, locale)) { |
| 59 | + return obj[locale]; |
| 60 | + } else if (obj && typeof obj === "object") { |
| 61 | + const result: any = {}; |
| 62 | + for (const key of Object.keys(obj)) { |
| 63 | + const value = extractTranslatables(obj[key], locale); |
| 64 | + if ( |
| 65 | + (typeof value === "object" && |
| 66 | + value !== null && |
| 67 | + Object.keys(value).length > 0) || |
| 68 | + (Array.isArray(value) && value.length > 0) || |
| 69 | + (typeof value === "string" && value.length > 0) |
| 70 | + ) { |
| 71 | + result[key] = value; |
| 72 | + } |
| 73 | + } |
| 74 | + return result; |
| 75 | + } |
| 76 | + return undefined; |
| 77 | +} |
| 78 | + |
| 79 | +function isTranslatableObject(obj: any, locale: string): boolean { |
| 80 | + return ( |
| 81 | + obj && |
| 82 | + typeof obj === "object" && |
| 83 | + !Array.isArray(obj) && |
| 84 | + Object.prototype.hasOwnProperty.call(obj, locale) |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +function setNestedLocale( |
| 89 | + obj: any, |
| 90 | + path: string[], |
| 91 | + locale: string, |
| 92 | + value: string, |
| 93 | + originalLocale: string, |
| 94 | +) { |
| 95 | + let curr = obj; |
| 96 | + for (let i = 0; i < path.length - 1; i++) { |
| 97 | + const key = path[i]; |
| 98 | + if (!(key in curr)) curr[key] = {}; |
| 99 | + curr = curr[key]; |
| 100 | + } |
| 101 | + const last = path[path.length - 1]; |
| 102 | + if (curr[last] && typeof curr[last] === "object") { |
| 103 | + curr[last][locale] = value; |
| 104 | + // Reorder keys: source locale first, then others alphabetically |
| 105 | + if (originalLocale && curr[last][originalLocale]) { |
| 106 | + const entries = Object.entries(curr[last]); |
| 107 | + const first = entries.filter(([k]) => k === originalLocale); |
| 108 | + const rest = entries |
| 109 | + .filter(([k]) => k !== originalLocale) |
| 110 | + .sort(([a], [b]) => a.localeCompare(b)); |
| 111 | + const ordered = [...first, ...rest]; |
| 112 | + const reordered: Record<string, string> = {}; |
| 113 | + for (const [k, v] of ordered) { |
| 114 | + reordered[k] = v as string; |
| 115 | + } |
| 116 | + curr[last] = reordered; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments