Skip to content

Commit

Permalink
🐛 fix: Fix locale progress
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Nov 23, 2023
1 parent 05a1999 commit 69931ca
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
10 changes: 7 additions & 3 deletions packages/lobe-i18n/src/commands/TranslateLocale/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ class TranslateLocale {
to: item.to,
};
const { rerender, clear } = render(
<Progress isLoading={true} maxStep={1} progress={0} step={0} {...props} />,
<Progress hide isLoading maxStep={1} progress={0} step={0} {...props} />,
);
const data = await this.i18n.translate({
...item,
onProgress: (rest) => {
rerender(<Progress {...rest} {...props} />);
if (rest.maxStep > 0) {
rerender(<Progress {...rest} {...props} />);
} else {
clear();
}
},
});
clear();
if (data) {
if (data && Object.keys(data).length > 0) {
writeJSON(item.filename, data);
consola.success(chalk.yellow(relative(this.config.output, item.filename)));
}
Expand Down
10 changes: 7 additions & 3 deletions packages/lobe-i18n/src/commands/TranslateMarkdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,20 @@ class TranslateMarkdown {
};

const { rerender, clear } = render(
<Progress isLoading={true} maxStep={1} progress={0} step={0} {...props} />,
<Progress hide isLoading maxStep={1} progress={0} step={0} {...props} />,
);
const data = await this.i18n.translateMarkdown({
...item,
onProgress: (rest) => {
rerender(<Progress {...rest} {...props} />);
if (rest.maxStep > 0) {
rerender(<Progress {...rest} {...props} />);
} else {
clear();
}
},
});
clear();
if (data) {
if (data && Object.keys(data).length > 0) {
writeMarkdown(item.filename, data);
consola.success(chalk.yellow(relative('.', item.filename)));
}
Expand Down
5 changes: 4 additions & 1 deletion packages/lobe-i18n/src/components/Progress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import { onProgressProps } from '@/core/I18n';
interface ProgressProps extends onProgressProps {
filename: string;
from: string;
hide?: boolean;
to: string;
}

const Progress = memo<ProgressProps>(
({ filename, to, from, progress, maxStep, step, isLoading }) => {
({ hide, filename, to, from, progress, maxStep, step, isLoading }) => {
const theme = useTheme();

if (hide) return null;

return (
<SplitView flexDirection={'column'}>
<Text backgroundColor={theme.colorBgLayout} color={theme.colorText}>
Expand Down
8 changes: 4 additions & 4 deletions packages/lobe-i18n/src/core/I18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ export class I18n {
JSON.stringify(prompt),
);

if (splitString.length === 0) return;

this.maxStep = splitString.length;
this.step = 0;

if (splitString.length === 0) return;

onProgress?.({
isLoading: true,
maxStep: this.maxStep,
Expand Down Expand Up @@ -148,11 +148,11 @@ export class I18n {
});
const splitJson = splitJsonToChunks(this.config, entry, target, JSON.stringify(prompt));

if (splitJson.length === 0) return;

this.maxStep = splitJson.length;
this.step = 0;

if (splitJson.length === 0) return;

onProgress?.({
isLoading: true,
maxStep: this.maxStep,
Expand Down
3 changes: 0 additions & 3 deletions packages/lobe-i18n/src/utils/splitJsonToChunks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { consola } from 'consola';
import { isPlainObject, reduce } from 'lodash-es';

import { LocaleObj } from '@/types';
Expand Down Expand Up @@ -44,7 +43,6 @@ export const getSplitToken = (config: I18nConfig, prompt: string) => {
splitToken = config.splitToken;
}
splitToken = Math.floor(splitToken);
consola.info(`Split token: ${splitToken}`);
return splitToken;
};

Expand All @@ -56,7 +54,6 @@ export const splitJsonToChunks = (
): LocaleObj[] => {
const extraJSON = diffJson(entry, target);
const splitToken = getSplitToken(config, prompt);
consola.info(`Split token: ${splitToken}`);
const splitObj = splitJSONtoSmallChunks(extraJSON, splitToken);
return splitObj;
};

0 comments on commit 69931ca

Please sign in to comment.