@tuhama/translation-manager 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,55 +1,55 @@
1
- const lodash = require('lodash');
2
-
3
- /**
4
- * Utility functions for object manipulation.
5
- */
6
- class Utilities {
7
- /**
8
- * Recursively sorts object keys alphabetically.
9
- */
10
- static sortObject(obj) {
11
- if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
12
- return obj;
13
- }
14
-
15
- const sorted = {};
16
- Object.keys(obj).sort().forEach(key => {
17
- sorted[key] = Utilities.sortObject(obj[key]);
18
- });
19
- return sorted;
20
- }
21
-
22
- /**
23
- * Flattens a nested object into a set of dot-notated keys.
24
- */
25
- static flattenKeys(obj, prefix = '', keySet = new Set()) {
26
- Object.keys(obj).forEach(key => {
27
- const fullKey = prefix ? `${prefix}.${key}` : key;
28
- if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
29
- Utilities.flattenKeys(obj[key], fullKey, keySet);
30
- } else {
31
- keySet.add(fullKey);
32
- }
33
- });
34
- return keySet;
35
- }
36
-
37
- /**
38
- * Normalizes translation keys across all languages.
39
- */
40
- static syncKeys(translations, allKeys) {
41
- const languages = Object.keys(translations);
42
- languages.forEach(lang => {
43
- const content = translations[lang];
44
- allKeys.forEach(key => {
45
- if (lodash.get(content, key) === undefined) {
46
- lodash.set(content, key, '');
47
- }
48
- });
49
- translations[lang] = Utilities.sortObject(content);
50
- });
51
- return translations;
52
- }
53
- }
54
-
55
- module.exports = Utilities;
1
+ const lodash = require('lodash');
2
+
3
+ /**
4
+ * Utility functions for object manipulation.
5
+ */
6
+ class Utilities {
7
+ /**
8
+ * Recursively sorts object keys alphabetically.
9
+ */
10
+ static sortObject(obj) {
11
+ if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
12
+ return obj;
13
+ }
14
+
15
+ const sorted = {};
16
+ Object.keys(obj).sort().forEach(key => {
17
+ sorted[key] = Utilities.sortObject(obj[key]);
18
+ });
19
+ return sorted;
20
+ }
21
+
22
+ /**
23
+ * Flattens a nested object into a set of dot-notated keys.
24
+ */
25
+ static flattenKeys(obj, prefix = '', keySet = new Set()) {
26
+ Object.keys(obj).forEach(key => {
27
+ const fullKey = prefix ? `${prefix}.${key}` : key;
28
+ if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
29
+ Utilities.flattenKeys(obj[key], fullKey, keySet);
30
+ } else {
31
+ keySet.add(fullKey);
32
+ }
33
+ });
34
+ return keySet;
35
+ }
36
+
37
+ /**
38
+ * Normalizes translation keys across all languages.
39
+ */
40
+ static syncKeys(translations, allKeys) {
41
+ const languages = Object.keys(translations);
42
+ languages.forEach(lang => {
43
+ const content = translations[lang];
44
+ allKeys.forEach(key => {
45
+ if (lodash.get(content, key) === undefined) {
46
+ lodash.set(content, key, '');
47
+ }
48
+ });
49
+ translations[lang] = Utilities.sortObject(content);
50
+ });
51
+ return translations;
52
+ }
53
+ }
54
+
55
+ module.exports = Utilities;
@@ -1,51 +1,69 @@
1
- const axios = require('axios');
2
-
3
- /**
4
- * Service for interacting with Google Cloud Translation API.
5
- */
6
- class GoogleTranslator {
7
- constructor(apiKey) {
8
- this.apiKey = apiKey;
9
- this.baseUrl = 'https://translation.googleapis.com/language/translate/v2';
10
- }
11
-
12
- /**
13
- * Translates text or an array of texts.
14
- * @param {string|string[]} text - Text(s) to translate
15
- * @param {string} targetLang - Target language code (e.g. 'fr')
16
- * @param {string} sourceLang - Source language code (e.g. 'en')
17
- */
18
- async translate(text, targetLang, sourceLang = 'en') {
19
- if (!this.apiKey) {
20
- throw new Error('Google Translate API Key is missing. Please add it in Settings.');
21
- }
22
-
23
- if (!text || (Array.isArray(text) && text.length === 0)) {
24
- return text;
25
- }
26
-
27
- try {
28
- const response = await axios.post(
29
- `${this.baseUrl}?key=${this.apiKey}`,
30
- {
31
- q: text,
32
- target: targetLang,
33
- source: sourceLang,
34
- format: 'text'
35
- }
36
- );
37
-
38
- const translations = response.data.data.translations;
39
-
40
- if (Array.isArray(text)) {
41
- return translations.map(t => t.translatedText);
42
- }
43
- return translations[0].translatedText;
44
- } catch (error) {
45
- const message = error.response?.data?.error?.message || error.message;
46
- throw new Error(`Google Translate Error: ${message}`);
47
- }
48
- }
49
- }
50
-
51
- module.exports = GoogleTranslator;
1
+ const { Translate } = require('@google-cloud/translate').v3;
2
+
3
+ /**
4
+ * Service for interacting with Google Cloud Translation API v3.
5
+ */
6
+ class GoogleTranslator {
7
+ constructor(config) {
8
+ // Support both old API key format and new v3 config
9
+ if (typeof config === 'string') {
10
+ // Legacy API key - throw error to guide migration
11
+ throw new Error('Google Translate API v2 is deprecated. Please update your configuration to use v3 with projectId and keyFilename.');
12
+ }
13
+
14
+ this.projectId = config.projectId;
15
+ this.keyFilename = config.keyFilename;
16
+
17
+ if (!this.projectId) {
18
+ throw new Error('Google Cloud Project ID is required for Translate API v3. Please add it in Settings.');
19
+ }
20
+
21
+ // Initialize the translate client
22
+ const clientConfig = { projectId: this.projectId };
23
+ if (this.keyFilename) {
24
+ clientConfig.keyFilename = this.keyFilename;
25
+ }
26
+
27
+ this.translate = new Translate(clientConfig);
28
+ }
29
+
30
+ /**
31
+ * Translates text or an array of texts.
32
+ * @param {string|string[]} text - Text(s) to translate
33
+ * @param {string} targetLang - Target language code (e.g. 'fr')
34
+ * @param {string} sourceLang - Source language code (e.g. 'en')
35
+ */
36
+ async translate(text, targetLang, sourceLang = 'en') {
37
+ if (!this.projectId) {
38
+ throw new Error('Google Cloud Project ID is missing. Please add it in Settings.');
39
+ }
40
+
41
+ if (!text || (Array.isArray(text) && text.length === 0)) {
42
+ return text;
43
+ }
44
+
45
+ try {
46
+ const location = 'global';
47
+ const request = {
48
+ parent: `projects/${this.projectId}/locations/${location}`,
49
+ contents: Array.isArray(text) ? text : [text],
50
+ mimeType: 'text/plain',
51
+ sourceLanguageCode: sourceLang,
52
+ targetLanguageCode: targetLang,
53
+ };
54
+
55
+ const [response] = await this.translate.translateText(request);
56
+ const translations = response.translations.map(t => t.translatedText);
57
+
58
+ if (Array.isArray(text)) {
59
+ return translations;
60
+ }
61
+ return translations[0];
62
+ } catch (error) {
63
+ const message = error.message || 'Unknown translation error';
64
+ throw new Error(`Google Translate Error: ${message}`);
65
+ }
66
+ }
67
+ }
68
+
69
+ module.exports = GoogleTranslator;