Skip to content

Commit

Permalink
Merge pull request #10 from amir-deriv/master
Browse files Browse the repository at this point in the history
chore: fix docs and action yml workflow
  • Loading branch information
amir-deriv authored Apr 26, 2024
2 parents 107e7c6 + 70602e9 commit 7e931e4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 86 deletions.
86 changes: 2 additions & 84 deletions .github/actions/extract_and_sync_translations/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,92 +37,10 @@ runs:
node-version: '20'

- name: Install dependencies
run: |
npm install crc-32
run: npm install crc-32

- name: Run script
run: node -e `
const getRegexPattern = () =>
/(i18n_default_text={?|localize\()\s*(['"])\s*(.*?)(?<!\\)\2\s*/gs;

const getStringsFromInput = (input, i18n_marker = getRegexPattern()) => {
const messages = [];

let continue_loop = true;
while (continue_loop) {
const result = i18n_marker.exec(input);
if (result == null) continue_loop = false;
else {
const extracted = result[3];
// Replace escape characters.
messages.push(extracted.replace(/\\/g, ""));
}
}

return messages;
};

const getTranslatableFiles = () => {
const globs = ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"];
const file_paths = [];

for (let j = 0; j < globs.length; j++) {
let files_found = glob.sync(process.env.GITHUB_WORKSPACE + "/src/" + globs[j]);
files_found = files_found.filter(
(file_path) => file_path.indexOf("__tests__") === -1
);
file_paths.push(...files_found);
}

return file_paths;
};


const getKeyHash = (string) => crc32(string);

/** *********************************************
* Common
*/
const getKeyHash = (string) => crc32(string);

/** **********************************************
* Compile
*/
(async () => {
try {
const file_paths = getTranslatableFiles();
const messages = [];
const messages_json = {};

// Iterate over files and extract all strings from the i18n marker
for (let i = 0; i < file_paths.length; i++) {
console.log(file_paths[i]);

try {
const file = fs.readFileSync(file_paths[i], "utf8");
messages.push(...getStringsFromInput(file));
} catch (e) {
console.log(e);
}
}

// Hash the messages and set the key-value pair for json
for (let i = 0; i < messages.length; i++) {
messages_json[getKeyHash(messages[i])] = messages[i];
}

// Add to messages.json
fs.writeFileSync(
process.env.GITHUB_WORKSPACE + "/crowdin/messages.json",
JSON.stringify(messages_json),
"utf8",
(err) => console.log(err)
);
} catch (e) {
console.error(e);
}
})();
`
run: npx run-url https://raw.githubusercontent.com/deriv-com/translations/master/scripts/extract-translations.js

- name: Crowdin pull action to download translations
uses: crowdin/github-action@v1
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ npm install @deriv-com/translations
- pass default language to the `TranslationProvider` component.

```jsx
import { initializeI18n, TranslationProvider } from '@deriv/translations';
import { initializeI18n, TranslationProvider } from '@deriv-com/translations';
...
const i18nInstance = initializeI18n({ cdnUrl: 'https://cdn.example.com' })

Expand Down Expand Up @@ -128,7 +128,7 @@ The action takes following inputs:
- `R2_SECRET_ACCESS_KEY`: R2 secret access key from the Cloudflare R2 dashboard
- `R2_BUCKET_NAME`: R2 bucket name from the Cloudflare R2 dashboard

Refer to the action file [here](https://github.com/deriv-com/shared-actions/blob/master/.github/actions/sync_crowdin_translation_with_cloudflare/action.yml)
Refer to the action file [here](https://github.com/deriv-com/translations/blob/master/.github/actions/extract_and_sync_translations/action.yml)

## Contributing

Expand Down
80 changes: 80 additions & 0 deletions scripts/extract-translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const crc32 = require("crc-32").str;
const glob = require("glob");
const fs = require("fs");

const getRegexPattern = () =>
/(i18n_default_text={?|localize\()\s*(['"])\s*(.*?)(?<!\\)\2\s*/gs;

const getStringsFromInput = (input, i18n_marker = getRegexPattern()) => {
const messages = [];

let continue_loop = true;
while (continue_loop) {
const result = i18n_marker.exec(input);
if (result == null) continue_loop = false;
else {
const extracted = result[3];
// Replace escape characters.
messages.push(extracted.replace(/\\/g, ""));
}
}

return messages;
};

const getTranslatableFiles = () => {
const globs = ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"];
const file_paths = [];

for (let j = 0; j < globs.length; j++) {
let files_found = glob.sync(
`${process.env.GITHUB_WORKSPACE}/src/${globs[j]}`
);
files_found = files_found.filter(
(file_path) => file_path.indexOf("__tests__") === -1
);
file_paths.push(...files_found);
}

return file_paths;
};

const getKeyHash = (string) => crc32(string);

/** **********************************************
* Compile
*/
(async () => {
try {
const file_paths = getTranslatableFiles();
const messages = [];
const messages_json = {};

// Iterate over files and extract all strings from the i18n marker
for (let i = 0; i < file_paths.length; i++) {
console.log(file_paths[i]);

try {
const file = fs.readFileSync(file_paths[i], "utf8");
messages.push(...getStringsFromInput(file));
} catch (e) {
console.log(e);
}
}

// Hash the messages and set the key-value pair for json
for (let i = 0; i < messages.length; i++) {
messages_json[getKeyHash(messages[i])] = messages[i];
}

// Add to messages.json
fs.writeFileSync(
`${process.env.GITHUB_WORKSPACE}/crowdin/messages.json`,
JSON.stringify(messages_json),
"utf8",
(err) => console.log(err)
);
} catch (e) {
console.error(e);
}
})();

0 comments on commit 7e931e4

Please sign in to comment.