Skip to content

Commit f0a4fc0

Browse files
authored
#210 [firestore-translate-text] Rename variables to generic names (#337)
#210 [firestore-translate-text] Rename variables to generic names
2 parents 7327b97 + 6894ee0 commit f0a4fc0

File tree

11 files changed

+162
-162
lines changed

11 files changed

+162
-162
lines changed

firestore-translate-text/POSTINSTALL.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ To test out this extension, follow these steps:
66

77
1. If it doesn't exist already, create a collection called `${param:COLLECTION_PATH}`.
88

9-
1. Create a document with a field named `${param:MESSAGE_FIELD_NAME}` and make its value a word or phrase that you want to translate.
9+
1. Create a document with a field named `${param:INPUT_FIELD_NAME}` and make its value a word or phrase that you want to translate.
1010

11-
1. In a few seconds, you'll see a new field called `${param:TRANSLATIONS_FIELD_NAME}` pop up in the same document you just created; it will contain the translations.
11+
1. In a few seconds, you'll see a new field called `${param:OUTPUT_FIELD_NAME}` pop up in the same document you just created; it will contain the translations for each language specified.
1212

1313
### Using the extension
1414

15-
Whenever a string is written to the field `${param:MESSAGE_FIELD_NAME}` in `${param:COLLECTION_PATH}`, this extension translates the string into your specified target language(s). The source language of the string is automatically detected. This extension adds the translated string to `${param:TRANSLATIONS_FIELD_NAME}` in the same document using the following format:
15+
Whenever a string is written to the field `${param:INPUT_FIELD_NAME}` in `${param:COLLECTION_PATH}`, this extension translates the string into your specified target language(s). The source language of the string is automatically detected. This extension adds the translated string to `${param:OUTPUT_FIELD_NAME}` in the same document using the following format:
1616

1717
```
1818
{
19-
${param:MESSAGE_FIELD_NAME}: 'My name is Bob',
20-
${param:TRANSLATIONS_FIELD_NAME}: {
19+
${param:INPUT_FIELD_NAME}: 'My name is Bob',
20+
${param:OUTPUT_FIELD_NAME}: {
2121
de: 'Ich heiße Bob',
2222
en: 'My name is Bob',
2323
es: 'Mi nombre es Bob',
@@ -26,7 +26,7 @@ Whenever a string is written to the field `${param:MESSAGE_FIELD_NAME}` in `${pa
2626
}
2727
```
2828

29-
If the `${param:MESSAGE_FIELD_NAME}` field of the document is updated, then the translations will be automatically updated, as well.
29+
If the `${param:INPUT_FIELD_NAME}` field of the document is updated, then the translations will be automatically updated, as well.
3030

3131
### Monitoring
3232

firestore-translate-text/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
* Collection path: *What is the path to the collection that contains the strings that you want to translate?
1717
*
1818

19-
* Message field name: *What is the name of the field that contains the strings that you want to translate?
19+
* Input field name: *What is the name of the field that contains the string that you want to translate?
2020
*
2121

22-
* Translations field name: *What is the name of the field where you want to store your translations?
22+
* Output field name: *What is the name of the field where you want to store your translations?
2323
*
2424

2525

firestore-translate-text/extension.yaml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ params:
8181
value: asia-northeast1
8282
default: us-central1
8383
required: true
84+
8485
- param: LANGUAGES
8586
label: Target languages for translations, as a comma-separated list
8687
description: >
8788
Into which target languages do you want to translate new strings?
88-
The languages are identifed using ISO-639-1 codes in a comma-separated list, for example: en,es,de,fr.
89+
The languages are identified using ISO-639-1 codes in a comma-separated list, for example: en,es,de,fr.
8990
For these codes, visit the [supported languages list](https://cloud.google.com/translate/docs/languages).
9091
default: en,es,de,fr
9192
example: en,es,de,fr
@@ -98,24 +99,24 @@ params:
9899
label: Collection path
99100
description: >
100101
What is the path to the collection that contains the strings that you want to translate?
101-
default: messages
102-
example: messages
102+
default: translations
103+
example: translations
103104
validationRegex: "^[^/]+(/[^/]+/[^/]+)*$"
104105
validationErrorMessage: Must be a valid Cloud Firestore Collection
105106
required: true
106107

107-
- param: MESSAGE_FIELD_NAME
108-
label: Message field name
108+
- param: INPUT_FIELD_NAME
109+
label: Input field name
109110
description: >
110-
What is the name of the field that contains the strings that you want to translate?
111-
default: message
112-
example: message
111+
What is the name of the field that contains the string that you want to translate?
112+
default: input
113+
example: input
113114
required: true
114115

115-
- param: TRANSLATIONS_FIELD_NAME
116-
label: Translations field name
116+
- param: OUTPUT_FIELD_NAME
117+
label: Translations output field name
117118
description: >
118119
What is the name of the field where you want to store your translations?
119-
default: translations
120-
example: translations
120+
default: translated
121+
example: translated
121122
required: true

firestore-translate-text/functions/lib/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
1818
exports.default = {
1919
languages: process.env.LANGUAGES.split(","),
2020
location: process.env.LOCATION,
21-
messageFieldName: process.env.MESSAGE_FIELD_NAME,
22-
translationsFieldName: process.env.TRANSLATIONS_FIELD_NAME,
21+
inputFieldName: process.env.INPUT_FIELD_NAME,
22+
outputFieldName: process.env.OUTPUT_FIELD_NAME,
2323
};

firestore-translate-text/functions/lib/index.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
* limitations under the License.
1616
*/
1717
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1819
return new (P || (P = Promise))(function (resolve, reject) {
1920
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
2021
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
22+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
2223
step((generator = generator.apply(thisArg, _arguments || [])).next());
2324
});
2425
};
@@ -39,15 +40,15 @@ const translate = new translate_1.Translate({ projectId: process.env.PROJECT_ID
3940
// Initialize the Firebase Admin SDK
4041
admin.initializeApp();
4142
logs.init();
42-
exports.fstranslate = functions.handler.firestore.document.onWrite((change) => __awaiter(this, void 0, void 0, function* () {
43+
exports.fstranslate = functions.handler.firestore.document.onWrite((change) => __awaiter(void 0, void 0, void 0, function* () {
4344
logs.start();
44-
const { languages, messageFieldName, translationsFieldName } = config_1.default;
45-
if (validators.fieldNamesMatch(messageFieldName, translationsFieldName)) {
45+
const { languages, inputFieldName, outputFieldName } = config_1.default;
46+
if (validators.fieldNamesMatch(inputFieldName, outputFieldName)) {
4647
logs.fieldNamesNotDifferent();
4748
return;
4849
}
49-
if (validators.fieldNameIsTranslationPath(messageFieldName, translationsFieldName, languages)) {
50-
logs.messageFieldNameIsTranslationPath();
50+
if (validators.fieldNameIsTranslationPath(inputFieldName, outputFieldName, languages)) {
51+
logs.inputFieldNameIsOutputPath();
5152
return;
5253
}
5354
const changeType = getChangeType(change);
@@ -71,8 +72,8 @@ exports.fstranslate = functions.handler.firestore.document.onWrite((change) => _
7172
logs.error(err);
7273
}
7374
}));
74-
const extractMsg = (snapshot) => {
75-
return snapshot.get(config_1.default.messageFieldName);
75+
const extractInput = (snapshot) => {
76+
return snapshot.get(config_1.default.inputFieldName);
7677
};
7778
const getChangeType = (change) => {
7879
if (!change.after.exists) {
@@ -83,77 +84,76 @@ const getChangeType = (change) => {
8384
}
8485
return ChangeType.UPDATE;
8586
};
86-
const handleCreateDocument = (snapshot) => __awaiter(this, void 0, void 0, function* () {
87-
const msg = extractMsg(snapshot);
88-
if (msg) {
89-
logs.documentCreatedWithMsg();
87+
const handleCreateDocument = (snapshot) => __awaiter(void 0, void 0, void 0, function* () {
88+
const input = extractInput(snapshot);
89+
if (input) {
90+
logs.documentCreatedWithInput();
9091
yield translateDocument(snapshot);
9192
}
9293
else {
93-
logs.documentCreatedNoMsg();
94+
logs.documentCreatedNoInput();
9495
}
9596
});
9697
const handleDeleteDocument = () => {
9798
logs.documentDeleted();
9899
};
99-
const handleUpdateDocument = (before, after) => __awaiter(this, void 0, void 0, function* () {
100-
const msgAfter = extractMsg(after);
101-
const msgBefore = extractMsg(before);
102-
const msgHasChanged = msgAfter !== msgBefore;
103-
if (!msgHasChanged) {
104-
logs.documentUpdatedUnchangedMsg();
100+
const handleUpdateDocument = (before, after) => __awaiter(void 0, void 0, void 0, function* () {
101+
const inputAfter = extractInput(after);
102+
const inputBefore = extractInput(before);
103+
const inputHasChanged = inputAfter !== inputBefore;
104+
if (!inputHasChanged) {
105+
logs.documentUpdatedUnchangedInput();
105106
return;
106107
}
107-
if (msgAfter) {
108-
logs.documentUpdatedChangedMsg();
108+
if (inputAfter) {
109+
logs.documentUpdatedChangedInput();
109110
yield translateDocument(after);
110111
}
111-
else if (msgBefore) {
112-
logs.documentUpdatedDeletedMsg();
112+
else if (inputBefore) {
113+
logs.documentUpdatedDeletedInput();
113114
yield updateTranslations(after, admin.firestore.FieldValue.delete());
114115
}
115116
else {
116-
logs.documentUpdatedNoMsg();
117+
logs.documentUpdatedNoInput();
117118
}
118119
});
119-
const translateDocument = (snapshot) => __awaiter(this, void 0, void 0, function* () {
120-
const message = extractMsg(snapshot);
121-
logs.translateMsgAllLanguages(message, config_1.default.languages);
122-
const tasks = config_1.default.languages.map((targetLanguage) => __awaiter(this, void 0, void 0, function* () {
123-
const translatedMsg = yield translateMessage(message, targetLanguage);
120+
const translateDocument = (snapshot) => __awaiter(void 0, void 0, void 0, function* () {
121+
const input = extractInput(snapshot);
122+
logs.translateInputStringToAllLanguages(input, config_1.default.languages);
123+
const tasks = config_1.default.languages.map((targetLanguage) => __awaiter(void 0, void 0, void 0, function* () {
124124
return {
125125
language: targetLanguage,
126-
message: translatedMsg,
126+
output: yield translateString(input, targetLanguage),
127127
};
128128
}));
129129
try {
130130
const translations = yield Promise.all(tasks);
131-
logs.translateMsgAllLanguagesComplete(message);
131+
logs.translateInputToAllLanguagesComplete(input);
132132
const translationsMap = translations.reduce((output, translation) => {
133-
output[translation.language] = translation.message;
133+
output[translation.language] = translation.output;
134134
return output;
135135
}, {});
136136
yield updateTranslations(snapshot, translationsMap);
137137
}
138138
catch (err) {
139-
logs.translateMsgAllLanguagesError(message, err);
139+
logs.translateInputToAllLanguagesError(input, err);
140140
throw err;
141141
}
142142
});
143-
const translateMessage = (msg, targetLanguage) => __awaiter(this, void 0, void 0, function* () {
143+
const translateString = (string, targetLanguage) => __awaiter(void 0, void 0, void 0, function* () {
144144
try {
145-
logs.translateMsg(msg, targetLanguage);
146-
const [translatedMsg] = yield translate.translate(msg, targetLanguage);
147-
logs.translateMsgComplete(msg, targetLanguage);
148-
return translatedMsg;
145+
logs.translateInputString(string, targetLanguage);
146+
const [translatedString] = yield translate.translate(string, targetLanguage);
147+
logs.translateStringComplete(string, targetLanguage);
148+
return translatedString;
149149
}
150150
catch (err) {
151-
logs.translateMsgError(msg, targetLanguage, err);
151+
logs.translateStringError(string, targetLanguage, err);
152152
throw err;
153153
}
154154
});
155-
const updateTranslations = (snapshot, translations) => __awaiter(this, void 0, void 0, function* () {
155+
const updateTranslations = (snapshot, translations) => __awaiter(void 0, void 0, void 0, function* () {
156156
logs.updateDocument(snapshot.ref.path);
157-
yield snapshot.ref.update(config_1.default.translationsFieldName, translations);
157+
yield snapshot.ref.update(config_1.default.outputFieldName, translations);
158158
logs.updateDocumentComplete(snapshot.ref.path);
159159
});

firestore-translate-text/functions/lib/logs.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,59 +19,59 @@ const config_1 = require("./config");
1919
exports.complete = () => {
2020
console.log("Completed execution of extension");
2121
};
22-
exports.documentCreatedNoMsg = () => {
23-
console.log("Document was created without a string, no processing is required");
22+
exports.documentCreatedNoInput = () => {
23+
console.log("Document was created without an input string, no processing is required");
2424
};
25-
exports.documentCreatedWithMsg = () => {
26-
console.log("Document was created with a string");
25+
exports.documentCreatedWithInput = () => {
26+
console.log("Document was created with an input string");
2727
};
2828
exports.documentDeleted = () => {
2929
console.log("Document was deleted, no processing is required");
3030
};
31-
exports.documentUpdatedChangedMsg = () => {
32-
console.log("Document was updated, string has changed");
31+
exports.documentUpdatedChangedInput = () => {
32+
console.log("Document was updated, input string has changed");
3333
};
34-
exports.documentUpdatedDeletedMsg = () => {
35-
console.log("Document was updated, string was deleted");
34+
exports.documentUpdatedDeletedInput = () => {
35+
console.log("Document was updated, input string was deleted");
3636
};
37-
exports.documentUpdatedNoMsg = () => {
38-
console.log("Document was updated, no string exists, no processing is required");
37+
exports.documentUpdatedNoInput = () => {
38+
console.log("Document was updated, no input string exists, no processing is required");
3939
};
40-
exports.documentUpdatedUnchangedMsg = () => {
41-
console.log("Document was updated, string has not changed, no processing is required");
40+
exports.documentUpdatedUnchangedInput = () => {
41+
console.log("Document was updated, input string has not changed, no processing is required");
4242
};
4343
exports.error = (err) => {
4444
console.error("Failed execution of extension", err);
4545
};
4646
exports.fieldNamesNotDifferent = () => {
47-
console.error("The `Message` and `Translations` field names must be different for this extension to function correctly");
47+
console.error("The `Input` and `Output` field names must be different for this extension to function correctly");
4848
};
4949
exports.init = () => {
5050
console.log("Initializing extension with configuration", config_1.default);
5151
};
52-
exports.messageFieldNameIsTranslationPath = () => {
53-
console.error("The `Message` field name must not be the same as a `Translation` path for this extension to function correctly");
52+
exports.inputFieldNameIsOutputPath = () => {
53+
console.error("The `Input` field name must not be the same as an `Output` path for this extension to function correctly");
5454
};
5555
exports.start = () => {
5656
console.log("Started execution of extension with configuration", config_1.default);
5757
};
58-
exports.translateMsg = (msg, language) => {
59-
console.log(`Translating msg: '${msg}' into language(s): '${language}'`);
58+
exports.translateInputString = (string, language) => {
59+
console.log(`Translating string: '${string}' into language(s): '${language}'`);
6060
};
61-
exports.translateMsgComplete = (msg, language) => {
62-
console.log(`Finished translating msg: '${msg}' into language(s): '${language}'`);
61+
exports.translateStringComplete = (string, language) => {
62+
console.log(`Finished translating string: '${string}' into language(s): '${language}'`);
6363
};
64-
exports.translateMsgError = (msg, language, err) => {
65-
console.error(`Error when translating msg: '${msg}' into language(s): '${language}'`, err);
64+
exports.translateStringError = (string, language, err) => {
65+
console.error(`Error when translating string: '${string}' into language(s): '${language}'`, err);
6666
};
67-
exports.translateMsgAllLanguages = (msg, languages) => {
68-
console.log(`Translating msg: '${msg}' into language(s): '${languages.join(",")}'`);
67+
exports.translateInputStringToAllLanguages = (string, languages) => {
68+
console.log(`Translating string: '${string}' into language(s): '${languages.join(",")}'`);
6969
};
70-
exports.translateMsgAllLanguagesComplete = (msg) => {
71-
console.log(`Finished translating msg: '${msg}'`);
70+
exports.translateInputToAllLanguagesComplete = (string) => {
71+
console.log(`Finished translating string: '${string}'`);
7272
};
73-
exports.translateMsgAllLanguagesError = (msg, err) => {
74-
console.error(`Error when translating msg: '${msg}'`, err);
73+
exports.translateInputToAllLanguagesError = (string, err) => {
74+
console.error(`Error when translating string: '${string}'`, err);
7575
};
7676
exports.updateDocument = (path) => {
7777
console.log(`Updating Cloud Firestore document: '${path}'`);

firestore-translate-text/functions/lib/validators.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
Object.defineProperty(exports, "__esModule", { value: true });
1818
exports.fieldNamesMatch = (field1, field2) => field1 === field2;
19-
exports.fieldNameIsTranslationPath = (field, translationsField, languages) => {
19+
exports.fieldNameIsTranslationPath = (inputFieldName, outputFieldName, languages) => {
2020
for (const language of languages) {
21-
if (field === `${translationsField}.${language}`) {
21+
if (inputFieldName === `${outputFieldName}.${language}`) {
2222
return true;
2323
}
2424
}

firestore-translate-text/functions/src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
export default {
1818
languages: process.env.LANGUAGES.split(","),
1919
location: process.env.LOCATION,
20-
messageFieldName: process.env.MESSAGE_FIELD_NAME,
21-
translationsFieldName: process.env.TRANSLATIONS_FIELD_NAME,
20+
inputFieldName: process.env.INPUT_FIELD_NAME,
21+
outputFieldName: process.env.OUTPUT_FIELD_NAME,
2222
};

0 commit comments

Comments
 (0)