npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

string-language-translator

v1.1.0

Published

String Language Translator. A library to translate strings in code files using the Google Generative AI API.

Downloads

7

Readme

STRING LANGUAGE TRANSLATOR

This library allows users to extract and translate user interface strings from a code file to a specified language. The library reads any code file containing English text, identifies user interface strings, translates them to the desired language using the Google Generative AI API (Gemini model), and outputs a JSON file compatible with the i18next internationalization library.

The code files used as input may be of any of the popular programming languages that use "" or '' to represent strings.

PREREQUISITES

  1. Node.js installed on your machine.
  2. A valid API key for Google Generative AI. However, as of now, I will provide you with an API key of my own in the installation procedure below.
  3. An internet connection.

INSTALLATION

  1. Run npm install string-language-translator to install the package and any dependencies.
  2. Create a .env file in the same directory and type the API key as shown below: API_KEY="AIzaSyAd5oMMQ0Wc08u3SOB_3OR4jLjyuO47TTQ"

USAGE

  1. Set File Path and Language: Define the path to the code file and set the target language in generateTranslations()

Sample Code in JavaScript

const translator = require("string-language-translator");

const filePath = './Home/index.java'; // Path to your code file const targetLanguage = 'French'; // Target translation language

translator.generateTranslations(filePath, targetLanguage);

OUTPUT

A JSON file is created in the same directory as the code file with key-value pairs where each key represents a phrase (e.g., phrase_1) and each value is the translated text.

Example output in French_translation.json:

{ "phrase_1": "Comment ça va?", "phrase_2": "Merci beaucoup", ... }

GOOGLE GENERATIVE AI (Gemini) MODEL

The Gemini model in this library serves as a multilingual translation engine. Through this model, the library ensures accurate, contextual translations of user interface strings to the specified language.

ERROR HANDLING

If the translation fails for a phrase, the library logs the error and defaults to the original text for that phrase.


Below is sample code to see how the i18next package comes into play after usage of string-language-translator.

//English_translation.json from string-language-translator, you can rename this to translation.json { "welcome": "Welcome", "login": "Login", "logout": "Logout" }

//Spanish_translation.json from string-language-translator, you can rename this to translation.json { "welcome": "Bienvenido", "login": "Iniciar sesión", "logout": "Cerrar sesión" }

import { useTranslation } from 'react-i18next';

import i18next from 'i18next'; import LanguageDetector from 'i18next-browser-languagedetector'; import HttpBackend from 'i18next-http-backend';

i18next .use(HttpBackend) // to load translations from your server .use(LanguageDetector) // to detect the user's language .init({ fallbackLng: 'en', // default language, e.g '/locales/en/translation.json' debug: true, backend: { loadPath: '/locales/{{lng}}/translation.json', // where to find the translations }, interpolation: { escapeValue: false, // react already safes from xss }, });

const MyComponent = () => { const { t } = useTranslation();

const changeLanguage = (language) => { i18next.changeLanguage(language); // Change language dynamically....{{lng}} from the path above };

return ( {t('welcome')} {t('login')} ); };

SUMMARY of what happens in the library... File Parsing: Open and read the input code file, then scan for strings (i.e., the parts of the code that would be displayed to the user). Translation: Use the Google Generative AI model to translate each extracted phrase into the specified language. JSON Generation: Create a JSON file with the translated text in a format compatible with i18next.

For every first api call... [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent: fetch failed

The first translation iteration always throws the error above. This should not worry you as it has been catered for in the library code. Later versions may solve this error.