Commit 4c2b8c18 by Walter Huang

feat: i18n fetch from sheet

parents
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Caches
.cache
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store
# google sheet
cred.json
# i18n-puller
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```
This project was created using `bun init` in bun v1.1.18. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
File added
{
"type": "service_account",
"project_id": "just-resolver-233205",
"private_key_id": "e7e09623f4d82fadc61c5b4c0260aef09e14715f",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDwF8CvZTRT/GfS\nHlDJJM8mBQbCTL434T5LFKsDSp116fe0r8gd2slPxL0Lnkr1kV5Aw4AhoQUxgtXQ\na+FaaCFvXir49SudMR1KB42NrvjDTmHJfqrGBNoy6FVrXv1LqHMCMvtHVuL9wG2T\nZ8iXp1CGbpIYNAiRhye2FR4bTnbJUxg8StrLbPLoGvm8Ad162SCimy2l3tnwiaTl\nx8jX0pR5aGxjs7VRtojvbYrqJGmGYlA1+3CUgEcqr380kU4/Kq9vI2Wn/Lv2OhU1\n8ZGvuXT1golHNhFyTBKYCVjxwFLxRqNiyZhdgbu9vwxZ2+YcvNJgxRTLexixpCoz\nGCm1zpAhAgMBAAECggEABCuBcPpqwX+rhPJ6WDf/sgsZzjObhShwFsk93CGGYRcm\ncniwGtajMvO5jdNavW+gKbNLjSn5B15cnNFW2WVgGUlTKT5G2uTQu6msa3bLwsag\ne6mrySMp8ALpguMkNpVvI+1jQF6xtBMMxCfkC8A/7YnLl0iGEpL8nVS6EGLZPM9g\nSYre6O1LbDql4CaZU8ExyP6TYdcHZHqbWKhkCssjk9+oqjE11RcQK1o6GxamSsbP\nBrbLQDAe62kJMCE4u0ZgYR50vnLsg0EHRskeW9ywfZ7IIuwAYGokFTzeKBiLhx5l\nqInc6i5vbDnFKZp1soa+dle79qNCsrasn9yKgxzJTQKBgQD/T9UOxBACUppolMjj\nIzlPKvxr+LFefnigPuV7ttQezlORqwKYeIyq19VSBNd4dj12/r6zu+PCuCU6D4LM\no15FwhtKf6BBKvmXZvcq3I7sThZUE4g8BKrg9WlxpRKeO3p3iFDFkC408ocKa3+U\nG6IlkJiWh9vOA0U2WY1FPWndLQKBgQDwvWtLFx3jI/b08dAewPz6E5/L31oS6x9l\nMJYDUWuuSSM9YGNKGXRA2FzpmfSi7LLPFwDroXtgLJqqildZ22w7AkCIQj52l9n7\nveioY7bL1NlJOp7BCW4HNh5EwaQQNdexEaAi6kKw+94gUmqlj21Ur744WNNWm2Ue\nH8nlDeOfRQKBgAo5513FCqMdLeQceYNniMccAKpOdsRDKo+4lTaEVzC4sO43vE3h\nHjlFldzIJt1DVyjqATdvwiqtd4hA0Mtji8YdhTxRqliR4RdoX2QPG4Zphklt0toc\nkb+a9NNBemJuHXSmOB4BrWgfgrsvHRgQl0kL5qC45lv1E5OeuKLaV+AtAoGBAMR3\nEVTarL1t4GDN5zbx8IJ1+Xb79Bp0X87I8+QJUoONlIDGZMFYPJOe/1R1HDzKRrLS\n5yFlS7aVMDP1aowekhkmIVwSE7/c9TyJI5Iorklyj1E/0cdf1rdz5wfdr/K6vif3\nZmSYIY0mP4TJPdeXUXZHZkm909tePgtzjeSsjRKxAoGBAKqxk8maYtZ3WaUOQjTp\nqcs0H29OSide0w/zdXfQS+zR99T5K1DNKphHUh8NO4EY8rwHJE7zxTqoX4wDMaeA\nAPnIwEJrycsqCyVaj0Ynr/hXKGHNW5yYJTsKpKyZk6K0gtGFzPXUaP4Y0jxPVNNH\n9dh1Bs38A2aA96yTux+emylI\n-----END PRIVATE KEY-----\n",
"client_email": "asb-templates-data@just-resolver-233205.iam.gserviceaccount.com",
"client_id": "100514477753137560622",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/asb-templates-data%40just-resolver-233205.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
\ No newline at end of file
console.log("Hello via Bun!");
\ No newline at end of file
{
"name": "i18n-puller",
"module": "index.ts",
"type": "module",
"scripts": {
"fetch_i18n_sheet_and_create_mr": "bun run src/tasks/pull/index.ts"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"googleapis": "^152.0.0",
"opencc-js": "^1.0.5",
"path": "^0.12.7"
}
}
\ No newline at end of file
// checkIsThisLocale.ts
import * as OpenCC from 'opencc-js';
type Locale = "zh" | "zh-hant" | "en";
function isEnglishOnly(text: string): boolean {
return /^[a-zA-Z\s.,!?'"()]+$/.test(text);
}
/**
* 检查文本是否完全属于指定语言
* @param text 输入文本
* @param locale "zh" | "zh-hant" | "en"
* @returns boolean
*/
export default async function checkIsThisLocale(text: string, locale: Locale, debug?: Boolean): Promise<boolean | any> {
const converter1 = OpenCC.Converter({ from: 'hk', to: 'cn' });
const converter2 = OpenCC.Converter({ from: 'cn', to: 'hk' });
const simplified = converter1(text)
const traditional = converter2(text);
switch (locale) {
case "zh":
if (debug) {
return {
result: text === simplified && !/[a-zA-Z]/.test(text),
convert: simplified
}
}
return text === simplified && !/[a-zA-Z]/.test(text);
case "zh-hant":
if (debug) {
return {
result: text === traditional && text !== simplified && !/[a-zA-Z]/.test(text),
convert: traditional
}
}
return text === traditional && text !== simplified && !/[a-zA-Z]/.test(text);
case "en":
return isEnglishOnly(text);
default:
throw new Error(`Unsupported locale: ${locale}`);
}
}
\ No newline at end of file
export async function fetchAzureOpenAIResponse(prompt: string): Promise<string> {
const endpoint = "https://strikingly-main-us-east.openai.azure.com/openai/deployments/gpt-4o-mini-20240718/chat/completions?api-version=2024-02-01";
const apiKey = "0f7c45a9023c4eac93f1547cbc75bd53";
const payload = {
messages: [
{ role: "user", content: prompt }
],
max_tokens: 4000,
temperature: 0.7,
top_p: 1,
response_format: { type: "json_object" }, // ✅ 用对象
presence_penalty: 0,
frequency_penalty: 0
};
try {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"api-key": apiKey
},
body: JSON.stringify(payload)
});
const text = await response.text(); // 先用 text 调试
if (!response.ok) {
console.error("Fetch error:", response.status, text);
return "";
}
const json = JSON.parse(text);
const content = json?.choices?.[0]?.message?.content ?? "";
// 替换所有 <span(?!>) 为 <span>
return content.replace(/<span(?!>)/g, "<span>");
} catch (error) {
console.error("Exception in fetchAzureOpenAIResponse:", error);
return "";
}
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed. Click to expand it.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment