Skip to content

Commit 8a19134

Browse files
authored
Release/0.0.3
2 parents e01616e + 146a83c commit 8a19134

File tree

8 files changed

+32
-57
lines changed

8 files changed

+32
-57
lines changed

.github/workflows/publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name: Publish
33
on:
44
release:
55
types: [published]
6+
branches:
7+
- main
68
workflow_dispatch:
79

810

@@ -31,8 +33,6 @@ jobs:
3133
3234
- name: Build package
3335
run: npm run build
34-
env:
35-
BASE_URL: ${{ secrets.BASE_URL }}
3636

3737
- name: Publish to NPM
3838
run: npm publish

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const MyComponent = () => {
6161

6262
return (
6363
<div>
64-
<h1>{t("Welcome to our app!", "static")}</h1>
64+
<h1>{t("Welcome to our app!", false)}</h1>
6565
<p>{t("This text will be automatically translated")}</p>
6666
</div>
6767
);
@@ -213,17 +213,17 @@ Returns an object with:
213213
- `loading`: Boolean indicating initialization of translations
214214
- `error`: Error object if translation loading failed
215215

216-
### Static persist
216+
### Persist for Editing
217217

218-
When you pass the 'static' parameter to the translation function, the translation will be persisted so that you can review and edit in the dashboard, default is non-static, nothing will be persisted.
218+
The 'persist' means the string will be persisted so that you can review and edit in the [dashboard](https://dashboard.autolocalise.com), default is true, if the content is dynamic or you don't want to see in the dashboard, pass 'false'.
219219

220220
```typescript
221221
import { useAutoTranslate } from "react-autolocalise";
222222
const MyComponent = () => {
223223
const { t } = useAutoTranslate();
224224
return (
225225
<div>
226-
<h1>{t("Welcome to our app!", "static")}</h1>
226+
<h1>{t("Welcome to our app!", false)}</h1>
227227
</div>
228228
);
229229
};

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "module",
33
"name": "react-autolocalise",
4-
"version": "0.0.2",
4+
"version": "0.0.3-alpha.1",
55
"description": "Auto-translation SDK for React and Next.js applications with SSR support",
66
"main": "dist/index.cjs",
77
"module": "dist/index.mjs",
@@ -74,5 +74,6 @@
7474
},
7575
"bugs": {
7676
"url": "https://github.com/AutoLocalise/react-autolocalise/issues"
77-
}
77+
},
78+
"homepage": "https://www.autolocalise.com"
7879
}

src/context/TranslationContext.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ const TranslationContext = createContext<TranslationContextType>({
1414
/**
1515
* Translates the given text to the target language
1616
* @param text - The text to translate
17-
* @param type - Optional parameter to specify the type of text being translated (e.g., 'title', 'description', etc.)
18-
* This is used by the translation service to provide context-aware translations
17+
* @param persist - Optional parameter to specify if the translation should be persisted
1918
* @returns The translated text, or the original text if translation is not yet available
2019
*/
2120
// eslint-disable-next-line @typescript-eslint/no-unused-vars
22-
translate: (text: string, type?: string) => text,
21+
translate: (text: string, persist: boolean = true, reference?: string) =>
22+
text,
2323
loading: true,
2424
error: null,
2525
});
@@ -109,7 +109,7 @@ export const TranslationProvider: React.FC<TranslationProviderSSRProps> = ({
109109
// Remove the translations state
110110
const translate = useMemo(
111111
() =>
112-
(text: string, type?: string): string => {
112+
(text: string, persist: boolean = true, reference?: string): string => {
113113
if (!text || loading) return text;
114114

115115
// Skip translation if source and target languages are the same
@@ -123,7 +123,7 @@ export const TranslationProvider: React.FC<TranslationProviderSSRProps> = ({
123123

124124
// Start async translation if not already pending
125125
if (!service.isTranslationPending(text)) {
126-
return service.translate(text, type);
126+
return service.translate(text, persist, reference);
127127
}
128128

129129
// Return original text while translation is pending
@@ -148,7 +148,8 @@ export const useAutoTranslate = () => {
148148
);
149149
}
150150
return {
151-
t: context.translate,
151+
t: (text: string, persist: boolean = true, reference?: string) =>
152+
context.translate(text, persist, reference),
152153
loading: context.loading,
153154
error: context.error,
154155
};

src/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ServerTranslation {
4040
const batchRequest = validTexts.map((text) => ({
4141
hashkey: this.service.generateHash(text),
4242
text,
43-
type: "static",
43+
persist: true,
4444
}));
4545

4646
try {

src/services/translation.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class TranslationService {
1111
private config: TranslationConfig;
1212
private cache: TranslationMap = {};
1313
private storage: StorageAdapter | null = null;
14-
private pendingTranslations: Map<string, string | undefined> = new Map();
14+
private pendingTranslations: Map<string, boolean> = new Map();
1515
private batchTimeout: NodeJS.Timeout | null = null;
1616
private cacheKey = "";
1717
private baseUrl = "https://autolocalise-main-53fde32.zuplo.app";
@@ -110,10 +110,11 @@ export class TranslationService {
110110
}
111111

112112
this.batchTimeout = setTimeout(async () => {
113-
const allTexts: { hashkey: string; text: string; type?: string }[] = [];
113+
const allTexts: { hashkey: string; text: string; persist: boolean }[] =
114+
[];
114115

115-
this.pendingTranslations.forEach((type, text) => {
116-
allTexts.push({ hashkey: this.generateHash(text), text, type });
116+
this.pendingTranslations.forEach((persist, text) => {
117+
allTexts.push({ hashkey: this.generateHash(text), text, persist });
117118
});
118119
this.pendingTranslations.clear();
119120

@@ -205,7 +206,11 @@ export class TranslationService {
205206
* Translate text asynchronously with batching (client-side)
206207
* This method is optimized for client-side usage where batching reduces API calls
207208
*/
208-
public translate(text: string, type?: string): string {
209+
public translate(
210+
text: string,
211+
persist: boolean = true,
212+
reference?: string
213+
): string {
209214
if (!text || !this.isInitialized) return text;
210215

211216
// Check cache first
@@ -214,7 +219,7 @@ export class TranslationService {
214219
return cachedTranslation;
215220
}
216221

217-
this.pendingTranslations.set(text, type);
222+
this.pendingTranslations.set(text, persist);
218223
this.scheduleBatchTranslation();
219224

220225
// Return original text while translation is pending
@@ -227,7 +232,7 @@ export class TranslationService {
227232
* @param texts Array of text items to translate in batch
228233
*/
229234
public async translateBatch(
230-
texts: { hashkey: string; text: string; type: string }[]
235+
texts: { hashkey: string; text: string; persist: boolean }[]
231236
): Promise<Record<string, string>> {
232237
if (!this.isInitialized || texts.length === 0) {
233238
return {};

src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export interface TranslationRequest {
1515
texts: Array<{
1616
hashkey: string;
1717
text: string;
18-
type?: string;
18+
persist: boolean;
19+
reference?: string;
1920
}>;
2021
sourceLocale: string;
2122
targetLocale: string;
@@ -33,7 +34,7 @@ export interface StorageAdapter {
3334
}
3435

3536
export interface TranslationContextType {
36-
translate: (text: string, type?: string) => string;
37+
translate: (text: string, persist: boolean, reference?: string) => string;
3738
loading: boolean;
3839
error: Error | null;
3940
}

src/types/index.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)