Skip to content

Commit

Permalink
[Tool] Add script to generate i18n resource array
Browse files Browse the repository at this point in the history
  • Loading branch information
schroda committed Apr 24, 2024
1 parent 082f923 commit bbb655c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"gql:codegen-base": "graphql-codegen --config gql_codegen.ts",
"gql:codegen-formatter": "ts-node -T tools/scripts/codegenFormatter.ts",
"gql:codegen": "yarn gql:codegen-base & yarn gql:codegen-formatter",
"i18n:gen-resources": "ts-node -T tools/scripts/generatei18nResources.ts",
"prepare": "husky"
},
"engines": {
Expand Down
28 changes: 28 additions & 0 deletions tools/scripts/generatei18nResources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import path from 'path';
import fs from 'fs';

const outputFilePath = path.join(__dirname, '../../src/i18n/index.ts');
const resourcesDirPath = path.join(__dirname, '../../public/locales');

const resourceFileNames = fs.readdirSync(resourcesDirPath);

const resourceNames = resourceFileNames.map((resourceFileName) =>
path.basename(resourceFileName, path.extname(resourceFileName)),
);

const outputFileContent = fs.readFileSync(outputFilePath, 'utf-8');

const updatedFileContent = outputFileContent.replace(
/(export const i18nResources = \[)[\s\S]*?(])/g,
`$1'${resourceNames.join("', '")}'$2`,
);

fs.writeFileSync(outputFilePath, updatedFileContent);

0 comments on commit bbb655c

Please sign in to comment.