New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

translator-module

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

translator-module

A customizable translation module

latest
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

Translator Module

A customizable translation module for Node.js applications. This module uses the Google Translate API to translate text between different languages.

Installation

To install the module, use npm:

npm install translator-module

Usage

Basic Usage

Here is a basic example of how to use the Translator class:

const Translator = require('translator-module');
const translator = new Translator('fr'); // Default target language

async function translateMessage(text, targetLang) {
    try {
        const translatedText = await translator.translateText(text, targetLang);
        console.log(`Translated text: ${translatedText}`);
    } catch (error) {
        console.error('Error translating text:', error);
    }
}

// Example usage
translateMessage('Hello, world!', 'es'); // Translate to Spanish

Discord Bot Example

Here is an example of how to use the Translator module in a Discord bot:

  • Install the necessary packages:
npm install discord.js translator-module
  • Create the bot file (bot.js):
const { Client, Intents } = require('discord.js');
const Translator = require('translator-module');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

const translator = new Translator('fr'); // Default target language

client.once('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', async message => {
    if (message.content.startsWith('!translate')) {
        const args = message.content.slice(11).trim().split(' ');
        const text = args.slice(1).join(' ');
        const targetLang = args[0] || 'fr';

        if (!text) {
            return message.channel.send('Usage: !translate <targetLang> <text>');
        }

        try {
            const translatedText = await translator.translateText(text, targetLang);
            message.channel.send(`Translated text: ${translatedText}`);
        } catch (error) {
            message.channel.send('Error translating text.');
        }
    }
});

client.login('YOUR_DISCORD_BOT_TOKEN');
  • Run the bot:
node bot.js

API

Translator Class

Constructor

  • Translator(defaultTargetLang: string): Creates a new Translator instance with a default target language.

Methods

  • translateText(text: string, targetLang?: string): Promise<string>: Translates the given text to the specified target language. If no target language is provided, the default target language is used.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the ISC License.

FAQs

Package last updated on 27 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts