Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: |Worker| support multi language #584

Merged
merged 1 commit into from
Feb 19, 2025
Merged

feat: |Worker| support multi language #584

merged 1 commit into from
Feb 19, 2025

Conversation

dreamhunter2333
Copy link
Owner

@dreamhunter2333 dreamhunter2333 commented Feb 19, 2025

PR Type

enhancement, dependencies


Description

  • Added multi-language support to the Worker.

  • Implemented language-specific messages in English and Chinese.

  • Updated dependencies and version numbers.

  • Included language context in API requests and responses.


Changes walkthrough 📝

Relevant files
Enhancement
15 files
worker.ts
Add multi-language support in Worker                                         
+31/-16 
index.ts
Add language support to mail API                                                 
+16/-9   
zh.ts
Add Chinese language messages                                                       
+24/-0   
en.ts
Add English language messages                                                       
+24/-0   
index.ts
Add language support to admin API                                               
+4/-1     
send_mail_api.ts
Add language support to send mail API                                       
+4/-1     
miniapp.ts
Add language support to Telegram miniapp                                 
+4/-1     
user.ts
Add language support to user API                                                 
+4/-1     
type.ts
Define type for locale messages                                                   
+20/-0   
types.d.ts
Add language context to types                                                       
+2/-0     
i18n.ts
Initialize i18n for frontend                                                         
+15/-0   
index.ts
Add language message retrieval logic                                         
+16/-0   
constants.ts
Update version constant                                                                   
+1/-1     
index.js
Include language header in API requests                                   
+4/-3     
main.js
Import i18n in main frontend file                                               
+1/-12   
Dependencies
5 files
pnpm-lock.yaml
Update dependencies                                                                           
+308/-308
package.json
Update version and dependencies                                                   
+6/-6     
package.json
Update version and dependencies                                                   
+4/-4     
package.json
Update version and dependencies                                                   
+2/-2     
package.json
Update version and dependencies                                                   
+2/-2     
Documentation
1 files
CHANGELOG.md
Update changelog for version 0.9.0                                             
+5/-1     
Additional files
2 files
pnpm-lock.yaml +208/-284
pnpm-lock.yaml +207/-199

Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Possible Issue

    The code sets the lang variable from the request headers but does not validate it against supported languages. This could lead to unexpected behavior if an unsupported language is provided.

    // save language in context
    const lang = c.req.raw.headers.get("x-lang");
    if (lang) { c.set("lang", lang); }
    const msgs = i18n.getMessages(lang || c.env.DEFAULT_LANG);
    Error Handling

    The error messages returned in various parts of the code are now dependent on the i18n messages. Ensure that all possible error scenarios are covered in the i18n files to avoid returning undefined messages.

    		return c.text(msgs.KVNotAvailableMsg, 400);
    	}
    	if (!getBooleanValue(c.env.ENABLE_WEBHOOK)) {
    		return c.text(msgs.WebhookNotEnabledMsg, 403);
    	}
    }
    if (!c.env.DB) {
    	return c.text(msgs.DBNotAvailableMsg, 400);
    }
    if (!c.env.JWT_SECRET) {
    	return c.text(msgs.JWTSecretNotSetMsg, 400);

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Handle unsupported language cases

    Ensure that i18n.getMessages handles cases where the lang parameter might not match
    any supported languages, to avoid potential runtime errors.

    worker/src/admin_api/index.ts [45]

    -const msgs = i18n.getMessages(lang);
    +const msgs = i18n.getMessages(lang) || i18n.getMessages(c.env.DEFAULT_LANG);
    Suggestion importance[1-10]: 8

    __

    Why: This suggestion ensures that the application does not encounter runtime errors when an unsupported language is provided. It adds a fallback mechanism to use the default language if the provided language is unsupported, enhancing robustness.

    Medium
    General
    Validate language codes

    Add a check to ensure c.get("lang") and c.env.DEFAULT_LANG are valid language codes
    before using them to fetch messages.

    worker/src/mails_api/index.ts [46]

    -const lang = c.get("lang") || c.env.DEFAULT_LANG;
    +const lang = ['en', 'zh'].includes(c.get("lang")) ? c.get("lang") : c.env.DEFAULT_LANG;
    Suggestion importance[1-10]: 7

    __

    Why: This suggestion adds validation for language codes, ensuring that only supported languages are used. This prevents potential issues with unsupported languages and improves the reliability of the language selection process.

    Medium
    Validate header language input

    Ensure that c.req.raw.headers.get("x-lang") is validated to be a supported language
    before setting it in the context.

    worker/src/worker.ts [30]

    -const lang = c.req.raw.headers.get("x-lang");
    +const lang = ['en', 'zh'].includes(c.req.raw.headers.get("x-lang")) ? c.req.raw.headers.get("x-lang") : c.env.DEFAULT_LANG;
    Suggestion importance[1-10]: 7

    __

    Why: This suggestion ensures that the language specified in the request headers is validated before being used. This prevents potential issues with unsupported languages and ensures that the application behaves as expected.

    Medium
    Validate locale value

    Add error handling for cases where i18n.global.locale.value might be undefined or
    not a supported language.

    frontend/src/api/index.js [27]

    -'x-lang': i18n.global.locale.value,
    +'x-lang': ['en', 'zh'].includes(i18n.global.locale.value) ? i18n.global.locale.value : 'en',
    Suggestion importance[1-10]: 7

    __

    Why: This suggestion adds validation for the locale value, ensuring that only supported languages are used. This prevents potential issues with unsupported languages and improves the reliability of the language selection process.

    Medium

    @dreamhunter2333 dreamhunter2333 merged commit 7889d2e into main Feb 19, 2025
    1 check passed
    @dreamhunter2333 dreamhunter2333 deleted the feature/dev branch February 19, 2025 16:37
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant