Skip to content

Commit a7ad8e8

Browse files
committed
fix bug:i18n
1 parent 11f0dd4 commit a7ad8e8

1 file changed

Lines changed: 65 additions & 13 deletions

File tree

src/pages/appstore.js

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import configManager from '../helpers/api/configManager';
2121

2222
const _ = cockpit.gettext;
2323
const language = cockpit.language;//获取cockpit的当前语言环境
24-
const DefaultImg = language === "zh_CN" ? DefaultImgzh : DefaultImgEn;
24+
25+
// 创建函数来获取默认图片,而不是常量
26+
const getDefaultImg = (currentLang) => {
27+
return currentLang === "zh_CN" ? DefaultImgzh : DefaultImgEn;
28+
};
2529

2630
var baseURL = ""
2731
const rootURL = `${window.location.hostname}`;
@@ -119,7 +123,7 @@ const AppImage = ({ src, alt, className, onError, isFirstScreen, width, height }
119123
};
120124

121125
//应用详情弹窗
122-
const AppDetailModal = ({ product, showFlag, onClose, isFavorite, onFavoriteUpdate, catalogClick }) => {
126+
const AppDetailModal = ({ product, showFlag, onClose, isFavorite, onFavoriteUpdate, catalogClick, currentLanguage }) => {
123127
const [index, setIndex] = useState(0); //用户图片浏览
124128
const [visible, setVisible] = useState(true); //用于显示安装选项:版本和应用名称
125129
const [customName, setCustomName] = useState(""); //用户存储用户输入的应用名称
@@ -377,7 +381,7 @@ const AppDetailModal = ({ product, showFlag, onClose, isFavorite, onFavoriteUpda
377381
src={`${baseURL}/media/logos/${imagName}`}
378382
alt=""
379383
className="app-icon"
380-
onError={(e) => (e.target.src = DefaultImg)}
384+
onError={(e) => (e.target.src = getDefaultImg(currentLanguage))}
381385
isFirstScreen={true}
382386
/>
383387
</a>
@@ -389,7 +393,7 @@ const AppDetailModal = ({ product, showFlag, onClose, isFavorite, onFavoriteUpda
389393
</h3>
390394
<a
391395
rel="noreferrer"
392-
href={`https://support.websoft9.com/${language === "zh_CN" ? '' : 'en/'}docs/${product.key || ''}`}
396+
href={`https://support.websoft9.com/${currentLanguage === "zh_CN" ? '' : 'en/'}docs/${product.key || ''}`}
393397
target="_blank"
394398
style={{ marginRight: '10px' }}
395399
>
@@ -454,7 +458,7 @@ const AppDetailModal = ({ product, showFlag, onClose, isFavorite, onFavoriteUpda
454458
<Carousel.Item key={item?.id} >
455459
<AppImage
456460
className="d-block"
457-
src={`${baseURL}/media/screenshots/${language === "zh_CN" ? "zh" : "en"}/${filename}`}
461+
src={`${baseURL}/media/screenshots/${currentLanguage === "zh_CN" ? "zh" : "en"}/${filename}`}
458462
alt={item?.key}
459463
width="100%"
460464
height="300px"
@@ -649,6 +653,7 @@ const AppStore = () => {
649653
const [isFavoriteAppsVisible, setFavoriteAppsIsVisible] = useState(true); // 是否显示 我的收藏 ,在收藏的情况下隐藏
650654
const [selectedMainCatalogKey, setSelectedMainCatalogKey] = useState("All"); //用于存储被选中的主目录
651655
const [selectedSubCatalogKey, setSelectedSubCatalogKey] = useState("All"); //用于存储被选中的子目录
656+
const [currentLanguage, setCurrentLanguage] = useState(cockpit.language); // 跟踪当前语言
652657

653658

654659
const initializeConfig = async () => {
@@ -676,7 +681,12 @@ const AppStore = () => {
676681
// 清理过期的本地缓存
677682
const cleanupExpiredCache = () => {
678683
try {
679-
const cacheKeys = ['appstore_data', 'favorite_apps_cache'];
684+
// 清理所有语言版本的缓存
685+
const cacheKeys = [
686+
'appstore_data_zh',
687+
'appstore_data_en',
688+
'favorite_apps_cache'
689+
];
680690
cacheKeys.forEach(key => {
681691
const timestamp = localStorage.getItem(key + '_timestamp');
682692
if (timestamp) {
@@ -820,9 +830,12 @@ const AppStore = () => {
820830
//获取所有apps(增强缓存机制)
821831
const getData = async () => {
822832
try {
833+
// 获取当前语言,确保使用最新的语言设置
834+
const currentLanguage = cockpit.language;
835+
const apiLanguage = currentLanguage === "zh_CN" ? "zh" : "en";
823836

824-
// 检查是否有缓存的应用数据(5分钟有效期)
825-
const cacheKey = 'appstore_data';
837+
// 检查是否有缓存的应用数据(5分钟有效期)- 包含语言信息的缓存键
838+
const cacheKey = `appstore_data_${apiLanguage}`;
826839
const cacheTimeout = 5 * 60 * 1000; // 5分钟
827840
const cachedData = localStorage.getItem(cacheKey);
828841
const cacheTimestamp = localStorage.getItem(cacheKey + '_timestamp');
@@ -840,8 +853,8 @@ const AppStore = () => {
840853

841854
// 缓存失效或不存在,重新获取数据
842855
const responses = await Promise.all([
843-
AppCatalog(language === "zh_CN" ? "zh" : "en"),
844-
AppAvailable(language === "zh_CN" ? "zh" : "en")
856+
AppCatalog(apiLanguage),
857+
AppAvailable(apiLanguage)
845858
]);
846859

847860
const [catalogResponse, productResponse] = responses;
@@ -862,7 +875,7 @@ const AppStore = () => {
862875
setApps(productResponse);
863876
setAppList(productResponse);
864877

865-
// 缓存数据
878+
// 缓存数据 - 使用包含语言信息的缓存键
866879
try {
867880
localStorage.setItem(cacheKey, JSON.stringify({
868881
catalogs: catalogSort,
@@ -907,6 +920,44 @@ const AppStore = () => {
907920
fetchData();
908921
}, []);
909922

923+
// 添加语言变化监听,当语言切换时清理缓存并重新获取数据
924+
useEffect(() => {
925+
const handleLanguageChange = async () => {
926+
const newLanguage = cockpit.language;
927+
928+
// 如果语言发生了变化
929+
if (newLanguage !== currentLanguage) {
930+
console.log('[AppStore] Language changed from', currentLanguage, 'to', newLanguage);
931+
setCurrentLanguage(newLanguage);
932+
933+
// 清理所有语言的缓存,确保获取正确语言的数据
934+
const cacheKeysToClean = ['appstore_data_zh', 'appstore_data_en'];
935+
cacheKeysToClean.forEach(key => {
936+
localStorage.removeItem(key);
937+
localStorage.removeItem(key + '_timestamp');
938+
});
939+
940+
// 重新获取数据
941+
setLoading(true);
942+
try {
943+
await getData();
944+
} catch (error) {
945+
setDataError(true);
946+
setErrorMessage(error.message || "Language change reload error");
947+
} finally {
948+
setLoading(false);
949+
}
950+
}
951+
};
952+
953+
// 创建一个定时器来检测语言变化
954+
const languageCheckTimer = setInterval(handleLanguageChange, 1000);
955+
956+
return () => {
957+
clearInterval(languageCheckTimer);
958+
};
959+
}, [currentLanguage]);
960+
910961
useEffect(() => {
911962
// 如果有 apps 并且 favoriteApps 为空或者存在
912963
if (apps && (favoriteApps || favoriteApps === "")) {
@@ -1044,7 +1095,7 @@ const AppStore = () => {
10441095
src={`${baseURL}/media/logos/${imageName}`}
10451096
alt={imageName}
10461097
className="app-icon"
1047-
onError={(e) => (e.target.src = DefaultImg)}
1098+
onError={(e) => (e.target.src = getDefaultImg(currentLanguage))}
10481099
isFirstScreen={isFirstScreen}
10491100
/>
10501101
</div>
@@ -1195,7 +1246,8 @@ const AppStore = () => {
11951246
onClose={handleClose}
11961247
isFavorite={isAppFavorite}
11971248
onFavoriteUpdate={onFavoriteUpdate}
1198-
catalogClick={handleCatalogClickForModal} />}
1249+
catalogClick={handleCatalogClickForModal}
1250+
currentLanguage={currentLanguage} />}
11991251
</>
12001252
);
12011253
};

0 commit comments

Comments
 (0)