diff --git a/README.md b/README.md index 79bb97f..9e74ef0 100644 --- a/README.md +++ b/README.md @@ -180,3 +180,35 @@ Use: | aria.zoomImage | Zoom Image | aria.zoomIn | Zoom In | aria.zoomOut | Zoom Out + +## Adding New Values + +To add a new value to the translation files, follow these steps: + +1. **Add to `en.json`**: First, add the new key-value pair to the `en.json` file. This file serves as the source for all translations. + +2. **Obtain Google Translate API Key**: You'll need a Google Translate API key to run the translation script. If you don't have one, follow Google's instructions to obtain an API key. + +3. **Update `script-translate.js`**: Once you have the API key, update the `script-translate.js` file. Locate the following line and replace `'REPLACEME'` with your actual API key: + + ```js + const apiKey = 'REPLACEME'; // Replace with your API key + ``` + + **Note**: Be careful not to commit this change to version control to keep your API key private. + +4. **Run the Translation Script**: After adding your new key to `en.json` and updating the API key, run the translation script: + + ```bash + node script-translate.js + ``` + + This script will: + - Read all language JSON files + - Identify the new key(s) added to `en.json` + - Translate the new value(s) to all other languages + - Update all language files with the new translations + +5. **Review Translations**: After running the script, it's a good practice to review the generated translations for accuracy. + +By following these steps, you ensure that new values are consistently added and translated across all language files in your project. \ No newline at end of file diff --git a/en.json b/en.json index 8f8f13e..e16b40f 100644 --- a/en.json +++ b/en.json @@ -126,7 +126,6 @@ "zoomImage": "Zoom Image", "zoomIn": "Zoom In", "zoomOut": "Zoom Out" - } } } diff --git a/script-genimports.js b/script-genimports.js new file mode 100644 index 0000000..672e5bf --- /dev/null +++ b/script-genimports.js @@ -0,0 +1,22 @@ +const fs = require('fs'); +const path = require('path'); + +// Set the directory where your JSON files are located +const jsonDir = '.'; + +// Read all files in the directory +fs.readdir(jsonDir, (err, files) => { + if (err) { + console.error(`Error reading directory: ${err}`); + return; + } + + // Filter out JSON files + files.filter(file => path.extname(file) === '.json').forEach(file => { + // Get the filename without the extension + const filename = path.basename(file, '.json').replace(/-/g, '_'); + + // Print the import statement + console.log(`import locale_${filename} from 'primelocale/${file}';`); + }); +}); \ No newline at end of file diff --git a/translate.js b/script-translate.js similarity index 73% rename from translate.js rename to script-translate.js index a3c1595..d7454b1 100644 --- a/translate.js +++ b/script-translate.js @@ -1,7 +1,11 @@ const fs = require('fs'); let promises = []; -// Function to read JSON file +/** + * Reads and parses a JSON file. + * @param {string} filename - The name of the file to read. + * @returns {Object|null} The parsed JSON object or null if an error occurs. + */ function readJSONFile(filename) { try { const data = fs.readFileSync(filename, 'utf8'); @@ -12,12 +16,19 @@ function readJSONFile(filename) { } } -// Function to write JSON file +/** + * Writes data to a JSON file after sorting it. + * @param {string} filename - The name of the file to write to. + * @param {Object} data - The data to write to the file. + * @returns {Promise} + */ async function writeJSONFile(filename, data) { try { + // Wait for all pending promises to settle await Promise.allSettled(promises); promises = []; + // Write sorted data to file fs.writeFileSync(filename, JSON.stringify(sortJsonObject(data), null, 2)); console.log(`Data written to ${filename}`); } catch (err) { @@ -25,6 +36,11 @@ async function writeJSONFile(filename, data) { } } +/** + * Sorts a JSON object recursively. + * @param {Object} obj - The object to sort. + * @returns {Object} A new sorted object. + */ function sortJsonObject(obj) { const keysWithSubobjects = []; const keysWithoutSubobjects = []; @@ -54,20 +70,23 @@ function sortJsonObject(obj) { return sortedObj; } - -// Function to translate text using Google Translate API +/** + * Translates text using Google Translate API. + * @param {string} text - The text to translate. + * @param {string} targetLanguage - The target language code. + * @returns {Promise} The translated text. + */ async function translateText(text, targetLanguage) { const apiKey = 'REPLACEME'; // Replace with your API key const url = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`; - + const response = await fetch(url, { method: 'POST', headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - charset: 'UTF-8', - - }, + Accept: 'application/json', + 'Content-Type': 'application/json', + charset: 'UTF-8', + }, body: JSON.stringify({ q: text, target: targetLanguage @@ -78,16 +97,23 @@ async function translateText(text, targetLanguage) { return data.data.translations[0].translatedText; } -// Function to recursively copy missing keys from source to destination +/** + * Recursively copies missing keys from source to destination, translating as necessary. + * @param {Object} sourceData - The source data object. + * @param {Object} destinationData - The destination data object. + * @param {string} targetLanguage - The target language code. + */ function copyMissingKeys(sourceData, destinationData, targetLanguage) { for (const key in sourceData) { if (!(key in destinationData)) { const englishText = sourceData[key]; - + + // Special handling for certain keys if (key === "am" || key === "pm" || key === "fileSizeTypes") { destinationData[key] = englishText; console.log(`Added key '${key}' with value '${englishText}'`); } else { + // Translate and add the key const promise = translateText(englishText, targetLanguage) .then(translation => { console.log(`Translated "${englishText}" to ${targetLanguage}: "${translation}"`); @@ -101,12 +127,17 @@ function copyMissingKeys(sourceData, destinationData, targetLanguage) { promises.push(promise); } } else if (typeof sourceData[key] === 'object' && typeof destinationData[key] === 'object') { + // Recursively handle nested objects copyMissingKeys(sourceData[key], destinationData[key], targetLanguage); } } } -// Function to copy missing keys from source to destination +/** + * Copies missing keys from source file to all destination files in a directory. + * @param {string} sourceFile - The path to the source file. + * @param {string} destinationDir - The path to the destination directory. + */ function copyMissingKeysRecursive(sourceFile, destinationDir) { const sourceData = readJSONFile(sourceFile); @@ -124,6 +155,7 @@ function copyMissingKeysRecursive(sourceFile, destinationDir) { files.forEach(file => { const destinationFile = `${destinationDir}/${file}`; + // Process only JSON files that are not the source file or package.json if (file.endsWith('.json') && !destinationFile.endsWith(sourceFile) && !file.endsWith('package.json')) { let destinationData = readJSONFile(destinationFile) || {}; @@ -131,11 +163,11 @@ function copyMissingKeysRecursive(sourceFile, destinationDir) { console.error(`Error reading ${destinationFile}. Skipping.`); return; } - + const sourceLanguage = Object.keys(sourceData)[0]; const destinationLanguage = Object.keys(destinationData)[0]; - if (!sourceLanguage || !destinationLanguage) { + if (!sourceLanguage || !destinationLanguage) { console.error("Root keys not found in source or destination file."); return; } @@ -154,4 +186,3 @@ const sourceFile = 'en.json'; const destinationDir = '.'; copyMissingKeysRecursive(sourceFile, destinationDir); -