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
- Node.js installed on your machine.
- 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.
- An internet connection.
INSTALLATION
- Run
npm install string-language-translator
to install the package and any dependencies. - Create a .env file in the same directory and type the API key as shown below: API_KEY="AIzaSyAd5oMMQ0Wc08u3SOB_3OR4jLjyuO47TTQ"
USAGE
- 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 (
); };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.