@sharpapi/sharpapi-node-translate 1.0.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.
package/README.md ADDED
@@ -0,0 +1,238 @@
1
+ ![SharpAPI GitHub cover](https://sharpapi.com/sharpapi-github-php-bg.jpg "SharpAPI Node.js Client")
2
+
3
+ # Advanced Text Translator API for Node.js
4
+
5
+ ## 🌐 Translate text with context-aware AI — powered by SharpAPI.
6
+
7
+ [![npm version](https://img.shields.io/npm/v/@sharpapi/sharpapi-node-translate.svg)](https://www.npmjs.com/package/@sharpapi/sharpapi-node-translate)
8
+ [![License](https://img.shields.io/npm/l/@sharpapi/sharpapi-node-translate.svg)](https://github.com/sharpapi/sharpapi-node-client/blob/master/LICENSE.md)
9
+
10
+ **SharpAPI Advanced Text Translator** provides context-aware translation that understands nuances, idioms, and industry-specific terminology. Supports multiple languages with high accuracy.
11
+
12
+ ---
13
+
14
+ ## 📋 Table of Contents
15
+
16
+ 1. [Requirements](#requirements)
17
+ 2. [Installation](#installation)
18
+ 3. [Usage](#usage)
19
+ 4. [API Documentation](#api-documentation)
20
+ 5. [Examples](#examples)
21
+ 6. [License](#license)
22
+
23
+ ---
24
+
25
+ ## Requirements
26
+
27
+ - Node.js >= 16.x
28
+ - npm or yarn
29
+
30
+ ---
31
+
32
+ ## Installation
33
+
34
+ ### Step 1. Install the package via npm:
35
+
36
+ ```bash
37
+ npm install @sharpapi/sharpapi-node-translate
38
+ ```
39
+
40
+ ### Step 2. Get your API key
41
+
42
+ Visit [SharpAPI.com](https://sharpapi.com/) to get your API key.
43
+
44
+ ---
45
+
46
+ ## Usage
47
+
48
+ ```javascript
49
+ const { SharpApiTranslateService } = require('@sharpapi/sharpapi-node-translate');
50
+
51
+ const apiKey = process.env.SHARP_API_KEY; // Store your API key in environment variables
52
+ const service = new SharpApiTranslateService(apiKey);
53
+
54
+ const text = 'Hello, how are you today?';
55
+ const targetLanguage = 'Spanish';
56
+
57
+ async function translateText() {
58
+ try {
59
+ // Submit translation job
60
+ const statusUrl = await service.translate(text, targetLanguage);
61
+ console.log('Job submitted. Status URL:', statusUrl);
62
+
63
+ // Fetch results (polls automatically until complete)
64
+ const result = await service.fetchResults(statusUrl);
65
+ console.log('Translation:', result.getResultJson());
66
+ } catch (error) {
67
+ console.error('Error:', error.message);
68
+ }
69
+ }
70
+
71
+ translateText();
72
+ ```
73
+
74
+ ---
75
+
76
+ ## API Documentation
77
+
78
+ ### Methods
79
+
80
+ #### `translate(text: string, targetLanguage: string, sourceLanguage?: string, context?: string): Promise<string>`
81
+
82
+ Translates text from source language to target language with optional context.
83
+
84
+ **Parameters:**
85
+ - `text` (string, required): The text to translate
86
+ - `targetLanguage` (string, required): The target language (e.g., 'Spanish', 'French', 'Japanese')
87
+ - `sourceLanguage` (string, optional): The source language (auto-detected if not specified)
88
+ - `context` (string, optional): Additional context to improve translation accuracy
89
+
90
+ **Returns:**
91
+ - Promise<string>: Status URL for polling the job result
92
+
93
+ **Example:**
94
+ ```javascript
95
+ const statusUrl = await service.translate(
96
+ 'The company is growing fast',
97
+ 'Spanish',
98
+ 'English',
99
+ 'Business context'
100
+ );
101
+ const result = await service.fetchResults(statusUrl);
102
+ ```
103
+
104
+ ### Response Format
105
+
106
+ The API returns the translated text:
107
+
108
+ ```json
109
+ {
110
+ "translated_text": "Hola, ¿cómo estás hoy?",
111
+ "source_language": "English",
112
+ "target_language": "Spanish",
113
+ "confidence": 0.98
114
+ }
115
+ ```
116
+
117
+ ---
118
+
119
+ ## Examples
120
+
121
+ ### Basic Translation
122
+
123
+ ```javascript
124
+ const { SharpApiTranslateService } = require('@sharpapi/sharpapi-node-translate');
125
+
126
+ const service = new SharpApiTranslateService(process.env.SHARP_API_KEY);
127
+
128
+ const englishText = 'Welcome to our website. We are glad you are here!';
129
+
130
+ service.translate(englishText, 'French')
131
+ .then(statusUrl => service.fetchResults(statusUrl))
132
+ .then(result => {
133
+ const translation = result.getResultJson();
134
+ console.log('Original:', englishText);
135
+ console.log('Translation:', translation.translated_text);
136
+ })
137
+ .catch(error => console.error('Translation failed:', error));
138
+ ```
139
+
140
+ ### Translation with Context
141
+
142
+ ```javascript
143
+ const service = new SharpApiTranslateService(process.env.SHARP_API_KEY);
144
+
145
+ const technicalText = 'The API returns a 404 error when the resource is not found.';
146
+
147
+ const statusUrl = await service.translate(
148
+ technicalText,
149
+ 'German',
150
+ 'English',
151
+ 'Software development and API documentation'
152
+ );
153
+
154
+ const result = await service.fetchResults(statusUrl);
155
+ console.log('Technical translation:', result.getResultJson().translated_text);
156
+ ```
157
+
158
+ ### Batch Translation
159
+
160
+ ```javascript
161
+ const service = new SharpApiTranslateService(process.env.SHARP_API_KEY);
162
+
163
+ const phrases = [
164
+ 'Good morning',
165
+ 'Thank you',
166
+ 'How much does it cost?',
167
+ 'Where is the nearest station?'
168
+ ];
169
+
170
+ const translations = await Promise.all(
171
+ phrases.map(async (phrase) => {
172
+ const statusUrl = await service.translate(phrase, 'Japanese');
173
+ const result = await service.fetchResults(statusUrl);
174
+ return {
175
+ original: phrase,
176
+ translated: result.getResultJson().translated_text
177
+ };
178
+ })
179
+ );
180
+
181
+ console.log('Translations:', translations);
182
+ ```
183
+
184
+ ---
185
+
186
+ ## Use Cases
187
+
188
+ - **Website Localization**: Translate website content for international audiences
189
+ - **Customer Support**: Provide multilingual customer service
190
+ - **E-commerce**: Translate product descriptions and reviews
191
+ - **Content Marketing**: Adapt marketing materials for different regions
192
+ - **Documentation**: Translate technical documentation and user guides
193
+ - **Mobile Apps**: Localize app content for global markets
194
+
195
+ ---
196
+
197
+ ## Supported Languages
198
+
199
+ SharpAPI supports translation between 100+ languages including:
200
+
201
+ English, Spanish, French, German, Italian, Portuguese, Russian, Chinese, Japanese, Korean, Arabic, Hindi, Dutch, Swedish, Polish, Turkish, and many more.
202
+
203
+ ---
204
+
205
+ ## API Endpoint
206
+
207
+ **POST** `/content/translate`
208
+
209
+ For detailed API specifications, refer to:
210
+ - [Postman Documentation](https://documenter.getpostman.com/view/31106842/2sBXVeGsVb)
211
+ - [Product Page](https://sharpapi.com/en/catalog/ai/content-marketing-automation/advanced-text-translator)
212
+
213
+ ---
214
+
215
+ ## Related Packages
216
+
217
+ - [@sharpapi/sharpapi-node-summarize-text](https://www.npmjs.com/package/@sharpapi/sharpapi-node-summarize-text) - Text summarization
218
+ - [@sharpapi/sharpapi-node-paraphrase](https://www.npmjs.com/package/@sharpapi/sharpapi-node-paraphrase) - Text paraphrasing
219
+ - [@sharpapi/sharpapi-node-proofread](https://www.npmjs.com/package/@sharpapi/sharpapi-node-proofread) - Grammar checking
220
+ - [@sharpapi/sharpapi-node-client](https://www.npmjs.com/package/@sharpapi/sharpapi-node-client) - Full SharpAPI SDK
221
+
222
+ ---
223
+
224
+ ## License
225
+
226
+ This project is licensed under the MIT License. See the [LICENSE.md](LICENSE.md) file for details.
227
+
228
+ ---
229
+
230
+ ## Support
231
+
232
+ - **Documentation**: [SharpAPI.com Documentation](https://sharpapi.com/documentation)
233
+ - **Issues**: [GitHub Issues](https://github.com/sharpapi/sharpapi-node-client/issues)
234
+ - **Email**: contact@sharpapi.com
235
+
236
+ ---
237
+
238
+ **Powered by [SharpAPI](https://sharpapi.com/) - AI-Powered API Workflow Automation**
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@sharpapi/sharpapi-node-translate",
3
+ "version": "1.0.0",
4
+ "description": "SharpAPI.com Node.js SDK for translating text",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "jest"
8
+ },
9
+ "keywords": [
10
+ "sharpapi",
11
+ "ai-powered",
12
+ "ai capabilities",
13
+ "api",
14
+ "ai api",
15
+ "api integration",
16
+ "artificial intelligence",
17
+ "natural language processing",
18
+ "restful api",
19
+ "nodejs",
20
+ "software development",
21
+ "content analysis",
22
+ "translation",
23
+ "text processing"
24
+ ],
25
+ "author": "Dawid Makowski <contact@sharpapi.com>",
26
+ "license": "MIT",
27
+ "dependencies": {
28
+ "@sharpapi/sharpapi-node-core": "file:../sharpapi-node-core"
29
+ },
30
+ "devDependencies": {
31
+ "jest": "^29.7.0"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "https://github.com/sharpapi/sharpapi-node-translate.git"
39
+ }
40
+ }
@@ -0,0 +1,27 @@
1
+ const { SharpApiCoreService, SharpApiJobTypeEnum } = require('@sharpapi/sharpapi-node-core');
2
+
3
+ /**
4
+ * Service for translating text using SharpAPI.com
5
+ */
6
+ class SharpApiTranslateService extends SharpApiCoreService {
7
+ /**
8
+ * Translates the provided text into selected language.
9
+ * Perfect for generating marketing introductions of longer texts.
10
+ *
11
+ * @param {string} text
12
+ * @param {string} language
13
+ * @param {string|null} voiceTone
14
+ * @param {string|null} context
15
+ * @returns {Promise<string>} - The status URL.
16
+ */
17
+ async translate(text, language, voiceTone = null, context = null) {
18
+ const data = { content: text, language };
19
+ if (voiceTone) data.voice_tone = voiceTone;
20
+ if (context) data.context = context;
21
+
22
+ const response = await this.makeRequest('POST', SharpApiJobTypeEnum.CONTENT_TRANSLATE.url, data);
23
+ return this.parseStatusUrl(response);
24
+ }
25
+ }
26
+
27
+ module.exports = { SharpApiTranslateService };
package/src/index.js ADDED
@@ -0,0 +1,6 @@
1
+ // sharpapi-node-translate/src/index.js
2
+ const { SharpApiTranslateService } = require('./SharpApiTranslateService');
3
+
4
+ module.exports = {
5
+ SharpApiTranslateService,
6
+ };