l-min-components 1.7.1535 → 1.7.1536
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.
- package/package.json +1 -1
- package/src/hooks/useTranslation.jsx +4 -31
package/package.json
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
import React, { useCallback, useContext, useEffect, useState } from "react";
|
|
2
2
|
import useAxios from "axios-hooks";
|
|
3
3
|
|
|
4
|
-
// Global cache for master translations
|
|
5
|
-
if (!window.translationCache) {
|
|
6
|
-
window.translationCache = {};
|
|
7
|
-
}
|
|
8
|
-
|
|
9
4
|
const environment = window.location.hostname.includes("staging")
|
|
10
5
|
? "learngual-bucket"
|
|
11
6
|
: "learngual-production";
|
|
12
|
-
const CACHE_DURATION = 60 * 60 * 1000; // 1 hour in milliseconds
|
|
13
7
|
const S3_BASE_URL = `https://${environment}.sfo3.digitaloceanspaces.com/${environment}/media/media/`;
|
|
14
8
|
|
|
15
9
|
const useTranslation = (initialSentences = []) => {
|
|
@@ -70,17 +64,9 @@ const useTranslation = (initialSentences = []) => {
|
|
|
70
64
|
};
|
|
71
65
|
|
|
72
66
|
/**
|
|
73
|
-
* Fetch master translations from S3
|
|
67
|
+
* Fetch master translations from S3
|
|
74
68
|
*/
|
|
75
|
-
const
|
|
76
|
-
const now = Date.now();
|
|
77
|
-
const cached = window.translationCache.master;
|
|
78
|
-
|
|
79
|
-
// Check if cache is valid and not expired
|
|
80
|
-
if (cached && cached.data && cached.expires > now) {
|
|
81
|
-
return cached.data;
|
|
82
|
-
}
|
|
83
|
-
|
|
69
|
+
const getMasterTranslations = async () => {
|
|
84
70
|
try {
|
|
85
71
|
// Fetch from S3 - master JSON contains all languages
|
|
86
72
|
const response = await fetch(
|
|
@@ -99,22 +85,9 @@ const useTranslation = (initialSentences = []) => {
|
|
|
99
85
|
|
|
100
86
|
console.log(masterTranslations, "MASTER");
|
|
101
87
|
|
|
102
|
-
// Cache in memory (single cache for all languages since JSON contains all)
|
|
103
|
-
window.translationCache.master = {
|
|
104
|
-
data: masterTranslations,
|
|
105
|
-
timestamp: now,
|
|
106
|
-
expires: now + CACHE_DURATION,
|
|
107
|
-
};
|
|
108
|
-
|
|
109
88
|
return masterTranslations;
|
|
110
89
|
} catch (error) {
|
|
111
90
|
console.error("Failed to fetch master translations:", error);
|
|
112
|
-
|
|
113
|
-
// Fallback to cached data if available (even if expired)
|
|
114
|
-
if (cached && cached.data) {
|
|
115
|
-
return cached.data;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
91
|
throw error;
|
|
119
92
|
}
|
|
120
93
|
};
|
|
@@ -150,8 +123,8 @@ const useTranslation = (initialSentences = []) => {
|
|
|
150
123
|
// Step 1: Fire-and-forget translation request to backend
|
|
151
124
|
sendTranslationRequestToBackend(language, words);
|
|
152
125
|
|
|
153
|
-
// Step 2: Get master translations
|
|
154
|
-
const masterTranslations = await
|
|
126
|
+
// Step 2: Get master translations from S3
|
|
127
|
+
const masterTranslations = await getMasterTranslations();
|
|
155
128
|
|
|
156
129
|
// Step 3: Extract only the translations we need
|
|
157
130
|
const relevantTranslations = extractRelevantTranslations(
|