I’m using Crowdin with react-i18next. Currently, to populate the supportedLngs
field of the i18next object, I have to use the fs
library to get the supported languages from the locales
folder. I think it would be nice to have Crowdin create a supported-langs.json
file that contains the supported languages. Alternatively, this information could also be added to the crowdin.yml
file.
Probably try this one, should be fine GitHub - lingui/js-lingui: 🌍📖 A readable, automated, and optimized (5 kb) internationalization for JavaScript
For the things you’ve mentioned, Crowdin brings you translated files by default. Like, is you upload english.json, on the export you will receive french.json, german.json and so on. Just need to configure the translation path so those files were not placed in .zip - French folder - french.json
Hey @DuuanVanVaagh. Thank you so much for your answer. My main problem was with i18next
and i18next-html-backend
not providing a way to get the supported languages (see Missing languages in the language list · Issue #1068 · i18next/i18next · GitHub). Because of these, users have to set the supportedLngs
option when they want to create a language picker.
I think it would be nice if Crowdin provides an option in the GitHub integration to write a simple JSON file containing the currently supported languages to the PR (see github-emoji-picker/supported_locales.json at main · rickstaa/github-emoji-picker · GitHub).
My current workaround is to use a GitHub action to create this file based on the language folders found in the public/locales
folder:
Action file
name: "Update supported locales"
on:
push:
branches: [main]
jobs:
updateSupportedLocales:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "16"
- name: Install dependencies
run: npm ci
- name: Update supported locales file
run: npm run update-locales
- name: Create Pull Request if 'src/assets/supported_locales.json' file was changed
uses: peter-evans/create-pull-request@v4
with:
commit-message: "Update supported Locales"
branch: "update_locals/patch"
delete-branch: true
title: Update supported locals
body: "The [update_supported_locales](https://github.com/rickstaa/github-emoji-picker/actions/workflows/update_supported_locales.yml) action found new locales."
labels: "ci, locales"
update-locales script file:
/**
* @file Compare 'assets/supported_locales.json' with the languages in 'public/locales'
* and create a pull request if there are new languages.
*/
const fs = require("fs/promises");
const run = async () => {
console.log("Get current locales...");
const currentLocales = await fs.readdir("public/locales");
console.log(currentLocales);
console.log(
"Get supported locales from 'src/assets/supported_locales.json'..."
);
let supportedLocales = JSON.parse(
await fs.readFile("src/assets/supported_locales.json", "utf-8")
).supportedLocales;
console.log(supportedLocales);
console.log("Compare current locales with set of supported locales...");
const newLocales = currentLocales.filter(
(locale) => !supportedLocales.includes(locale)
);
if (newLocales.length === 0) {
console.log("No new locales found.");
return;
}
console.log("New locales found:", newLocales);
console.log("Update 'assets/supported_locales.json'...");
const newSupportedLocales = {
supportedLocales: [...supportedLocales, ...newLocales],
};
await fs.writeFile(
"src/assets/supported_locales.json",
JSON.stringify(newSupportedLocales, null, 2)
);
console.log("'src/assets/supported_locales.json' updated.");
};
run();
Good to hear the Git actions works fine for you. Probably it’s yours framework specification or so, so native connector doesn’t work as expected. I know some users works outside or repositories, with API based w-flow only. I suppose creating the language .json file may be stated in feature request. If you have some extra pluses from other users, Crowdin will review this request and decides its future. Feature requests and ideas | Crowdin
Great, thanks for your answer.
@YuryChugov I just submitted the idea to Feature requests and ideas | Crowdin. It does not yet show up, but I think the Crowdin team has to approve it first. Will post a link here when it becomes visible.