This repository was archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetTranslation.js
More file actions
106 lines (84 loc) · 3.23 KB
/
getTranslation.js
File metadata and controls
106 lines (84 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { isEmpty, isPlainObject } = require("lodash");
const getTemplateLiteralExpressions = require("./getTemplateLiteralExpressions");
const getTemplateLiteralStrings = require("./getTemplateLiteralStrings");
const getTranslationParameters = require("./getTranslationParameters");
const findTranslationKey = require("./findTranslationKey");
const constants = require("./constants");
const escapeRegExp = (string) => string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const getTranslation = (string, context, translationsObject) => {
if (typeof string !== "string") {
throw new TypeError("String argument must be a string.");
}
if (string.length === 0) {
throw new TypeError("String argument must not be an empty string.");
}
if (typeof context !== "string") {
throw new TypeError("Context argument must be a string.");
}
if (context.length === 0) {
throw new TypeError("Context argument must not be an empty string.");
}
if (!isPlainObject(translationsObject)) {
throw new TypeError("translationsObject must be an object.");
}
if (isEmpty(translationsObject)) {
throw new TypeError("translationsObject must not be an empty object.");
}
// Get template literal expressions
const templateLiteralExpressions = getTemplateLiteralExpressions(string);
// Get template literal strings
const templateLiteralStrings = getTemplateLiteralStrings(
string,
templateLiteralExpressions
);
// Find translation string or object
const translationKeys = Object.keys(translationsObject);
const translationKey = findTranslationKey(
templateLiteralStrings,
translationKeys
);
// If no translation is found return original string
// (the string that need to be translated)
if (!translationKey) return string;
const translationValues = translationsObject[translationKey];
let translationTemplate = undefined;
// Let's see if translationValues is a string
if (typeof translationValues === "string") {
// Check for context
if (context !== constants.DEFAULT_TRANSLATION_CONTEXT) {
// Sorry, we can't assume context, return original string
return string;
} else {
// If the context is default
// translationValues IS the translation template
translationTemplate = translationValues;
}
}
// Let's see if translationValues is an object
// TODO: A little bit WET (not DRY), right? See above ^
// Refactor into a function, something like findTranslationValue
if (isPlainObject(translationValues)) {
if (Object.keys(translationValues).includes(context)) {
translationTemplate = translationValues[context];
} else {
// Couldn't find the specified context, we can't asume it
// Return original string
return string;
}
}
// Generate translation
const translationParameters = getTranslationParameters(translationKey);
let translation = translationTemplate;
translationParameters.forEach((translationParameter, index) => {
const translationParameterRegExp = new RegExp(
escapeRegExp(translationParameter.match),
"g"
);
translation = translation.replace(
translationParameterRegExp,
templateLiteralExpressions[index].match
);
});
return translation;
};
module.exports = getTranslation;