Simple Typescript wrapper for the GhanaNLP Translation API. Allows you to effortlessly translate text between supported languages and retrieve a list of available language pairs.
- Installation
- Getting Started
- Usage
- Error Handling
- API Reference
- Roadmap/Coming Soon
- Contributing
- License
Install the package via npm:
npm install @paakways/ghananlp-node
If you are using TypeScript, make sure to have axios
types as a development dependency:
npm install --save-dev @types/axios
-
Import the library into your Project
import {GhanaNLP} from '@paakways/ghananlp-node';
-
Initialize the library with your API Key and version
To use the library, you will need to get an API key from the GhanaNLP APIs website.
const api = new GhanaNLP('YOUR_API_KEY', 'v1');
To translate text from one language to another, use the translate
method. You need to specify the input text and the language pair code (in the format from-to
, e.g., en-tw
for English to Twi).
const translationRequest = { in: 'Hello World', lang: 'en-tw' };
try {
const response = await api.translate(translationRequest)
console.log('Translated text:', response.translatedText);
}
catch(error) {
console.error('Translation error:', error.message);
}
You can retrieve a list of all supported languages with their language codes:
try {
const languages = await api.getLanguages()
console.log('Supported languages:', languages);
}
catch(error) {
console.error('Error fetching languages:', error.message);
}
The library provides error handling to help diagnose issues with the API requests. If an error occurs, it will throw a message detailing the type and description of the error.
Example of catching an error:
try {
const response = await api.translate({ in: 'Hello', lang: 'invalid-code' })
}
catch(error) {
console.error('Error:', error.message); // Outputs detailed error message
}
Translates the given input text from one language to another.
-
Parameters:
-
request
: An object containing:-
in
: The input text to be translated (max 1000 characters). -
lang
: Language pair code in the formatfrom-to
(e.g.,en-tw
).
-
-
-
Returns: A promise that resolves to the translated text.
Retrieves the list of all supported languages.
-
Returns: A promise that resolves to an array of language objects, each containing:
-
code
: The language code (e.g.,en
for English). -
name
: The full language name (e.g.,English
).
-
- TTS/STT API integration
Contributions are welcome! If you find any bugs or have any feature requests, please open an issue or submit a pull request.
- Fork the repository.
- Create a new branch.
- Make your changes and commit them.
- Push your changes to the branch.
- Create a pull request.
This project is licensed under the MIT License.