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

more server i18n #31

Merged
merged 7 commits into from
Dec 15, 2024
Merged

more server i18n #31

merged 7 commits into from
Dec 15, 2024

Conversation

jokester
Copy link
Member

@jokester jokester commented Dec 15, 2024

  • workaround existing hardcoded string
  • server-only gettext variant
  • translate language names at server

Copy link

coderabbitai bot commented Dec 15, 2024

Walkthrough

This pull request enhances internationalization and localization capabilities throughout the project. Key changes include updates to the language model, translation extraction process, and translation files. A new hardcode_text function is introduced to capture hardcoded strings, and the language model is modified to utilize a new LanguageData TypedDict for better structure. Additionally, the Makefile is updated to improve translation string extraction, and numerous new language entries are added to the translation files.

Changes

File Change Summary
Makefile Updated babel-update-po command to include hardcode_text keyword; minor whitespace changes in comments.
app/models/language.py - Added LanguageData TypedDict
- Updated SYSTEM_LANGUAGES_DATA to use LanguageData
- Modified logging to use gettext for localization
- Adjusted error handling in clear method and to_api method for localization.
app/translations/__init__.py Added hardcode_text() and server_gettext() functions for improved string handling and locale management.
app/translations/en/LC_MESSAGES/messages.po Updated POT creation date and added numerous new translations for various languages.
app/translations/zh/LC_MESSAGES/messages.po Updated POT creation date, added new language entries without translations.

Possibly related PRs

  • More server i18n #30: Direct relationship with the hardcode_text keyword implementation in translation extraction and initialization.

Poem

🐰 A Hop of Words, a Rabbit's Cheer
With hardcode_text, our voices clear!
Languages bloom, from dusk till dawn,
In every tongue, our dreams are drawn.
Hooray for words, let them unite! 🌍✨

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. This feature will be included in our Pro Plan when released.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

Copy link

codecov bot commented Dec 15, 2024

Codecov Report

Attention: Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

Project coverage is 78.87%. Comparing base (56a6c87) to head (36106ff).
Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
app/models/language.py 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #31      +/-   ##
==========================================
- Coverage   79.75%   78.87%   -0.89%     
==========================================
  Files         109      110       +1     
  Lines        6689     6765      +76     
==========================================
+ Hits         5335     5336       +1     
- Misses       1354     1429      +75     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jokester jokester marked this pull request as ready for review December 15, 2024 08:07
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (2)
app/models/language.py (1)

32-782: Consider moving SYSTEM_LANGUAGES_DATA to a separate configuration file.

While the implementation is correct with proper use of hardcode_text for all language names, having such a large data structure in the model file affects readability and maintainability.

Consider moving this data to a separate configuration file (e.g., language_config.py) to improve code organization.

app/translations/en/LC_MESSAGES/messages.po (1)

1037-1467: Review and improve translations for clarity.

Some translations could be improved for better clarity and consistency:

  1. "Myanmar (Burmese)" should be just "Burmese" to match other language names
  2. "Scots Gaelic" should be "Scottish Gaelic" (more commonly used)
  3. Missing translation for "Language can not be deleted"

Apply these improvements:

-msgstr "Myanmar (Burmese)"
+msgstr "Burmese"

-msgstr "Scots Gaelic"
+msgstr "Scottish Gaelic"

msgid "Language can not be deleted"
-msgstr ""
+msgstr "Language cannot be deleted."
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f8d4009 and 9c3ea48.

📒 Files selected for processing (5)
  • Makefile (2 hunks)
  • app/models/language.py (6 hunks)
  • app/translations/__init__.py (1 hunks)
  • app/translations/en/LC_MESSAGES/messages.po (4 hunks)
  • app/translations/zh/LC_MESSAGES/messages.po (4 hunks)
🔇 Additional comments (7)
Makefile (1)

47-47: LGTM: Pybabel extraction command updated correctly.

The addition of -k hardcode_text to the pybabel extract command ensures that strings wrapped with the new hardcode_text function will be properly extracted for translation.

app/translations/__init__.py (2)

Line range hint 5-6: LGTM: Required imports added.

The imports are correctly added to support the new i18n functionality.


53-59: LGTM: Well-documented helper function for i18n.

The hardcode_text function is properly implemented as a pass-through function to mark strings for translation extraction.

app/models/language.py (4)

12-18: LGTM: Well-structured type definition.

The LanguageData TypedDict provides clear type hints for language data structure, improving code maintainability and type safety.


791-791: LGTM: Properly internationalized logging messages.

The logging messages are correctly wrapped with gettext for internationalization.

Also applies to: 805-805


900-900: LGTM: Internationalized error message.

The error message is properly wrapped with gettext for internationalization.


910-912: LGTM: Improved localized name handling.

The to_api method now correctly handles localized names with a proper fallback to the original name when translation is not available.

Comment on lines +1142 to +1145
msgid "豪萨语"
msgstr ""

#: app/models/language.py:292
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Review fuzzy translations.

Two translations are marked as fuzzy and need review:

  1. "弗里斯兰语" (Frisian)
  2. "印度尼西亚语" (Indonesian)

Please review and confirm these translations, then remove the fuzzy marker if they are correct.

Also applies to: 1203-1206

Comment on lines 1009 to 1437
msgid "斯洛伐克语"
msgstr ""

#: app/models/language.py:640
msgid "斯洛文尼亚语"
msgstr ""

#: app/models/language.py:647
msgid "索马里语"
msgstr ""

#: app/models/language.py:654
msgid "西班牙语"
msgstr ""

#: app/models/language.py:661
msgid "巽他语"
msgstr ""

#: app/models/language.py:668
msgid "斯瓦希里语"
msgstr ""

#: app/models/language.py:675
msgid "瑞典语"
msgstr ""

#: app/models/language.py:682
msgid "他加禄语(菲律宾语)"
msgstr ""

#: app/models/language.py:689
msgid "塔吉克语"
msgstr ""

#: app/models/language.py:696
msgid "泰米尔语"
msgstr ""

#: app/models/language.py:703
msgid "泰卢固语"
msgstr ""

#: app/models/language.py:710
msgid "泰语"
msgstr ""

#: app/models/language.py:717
msgid "土耳其语"
msgstr ""

#: app/models/language.py:724
msgid "乌克兰语"
msgstr ""

#: app/models/language.py:731
msgid "乌尔都语"
msgstr ""

#: app/models/language.py:738
msgid "乌兹别克语"
msgstr ""

#: app/models/language.py:745
msgid "越南语"
msgstr ""

#: app/models/language.py:752
msgid "威尔士语"
msgstr ""

#: app/models/language.py:759
msgid "科萨语"
msgstr ""

#: app/models/language.py:766
msgid "意第绪语"
msgstr ""

#: app/models/language.py:773
msgid "约鲁巴语"
msgstr ""

#: app/models/language.py:780
msgid "祖鲁语"
msgstr ""

#: app/models/language.py:791
msgid "Language collection already initialized."
msgstr ""

#: app/models/language.py:805
#, python-format
msgid "Initialized Language collection with %d languages"
msgstr ""

#: app/models/language.py:900
msgid "Language can not be deleted"
msgstr ""

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add missing translations for new language entries.

Multiple new language entries have been added but their translations are missing (empty msgstr). This includes entries for Japanese, English, Korean, and many other languages.

Please provide Chinese translations for all new language entries. For example:

msgid "日语"
-msgstr ""
+msgstr "日语"

msgid "英语"
-msgstr ""
+msgstr "英语"

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +1712 to +1724
msgstr "Project set configuration"

#: app/models/team.py:110
msgid "修改团队内项目集的设置"
msgstr "Modify the settings of the project collection within the team."
msgstr "Modify the settings of the project sets within the team."

#: app/models/team.py:113
msgid "删除项目集"
msgstr "Delete project set"

#: app/models/team.py:114
msgid "删除团队内的项目集"
msgstr "Delete the portfolio within the team."
msgstr "Delete the project sets within the team."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Maintain consistent punctuation in translations.

Some translations inconsistently use periods at the end. For example:

  • "Project set configuration" (no period)
  • "Modify the settings of the project sets within the team." (with period)

Apply consistent punctuation:

-msgstr "Project set configuration"
+msgstr "Project set configuration."

-msgstr "Delete project set"
+msgstr "Delete project set."
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
msgstr "Project set configuration"
#: app/models/team.py:110
msgid "修改团队内项目集的设置"
msgstr "Modify the settings of the project collection within the team."
msgstr "Modify the settings of the project sets within the team."
#: app/models/team.py:113
msgid "删除项目集"
msgstr "Delete project set"
#: app/models/team.py:114
msgid "删除团队内的项目集"
msgstr "Delete the portfolio within the team."
msgstr "Delete the project sets within the team."
msgstr "Project set configuration."
#: app/models/team.py:110
msgid "修改团队内项目集的设置"
msgstr "Modify the settings of the project sets within the team."
#: app/models/team.py:113
msgid "删除项目集"
msgstr "Delete project set."
#: app/models/team.py:114
msgid "删除团队内的项目集"
msgstr "Delete the project sets within the team."

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (5)
app/translations/__init__.py (2)

56-60: Enhance function documentation

While the function implementation is correct, the documentation could be more descriptive about its purpose in the i18n workflow.

 def hardcode_text(msgid: str) -> str:
     """
-    used to capture hardcoded string as msgid
+    Marks a hardcoded string for translation extraction.
+    This function helps identify strings that need to be included
+    in translation files without modifying the original text.
     """
     return msgid

63-65: Add error handling for locale operations

The function should handle potential exceptions from force_locale to ensure robustness.

 def server_gettext(msgid: str):
-    with flask_babel.force_locale(_app_config.BABEL_DEFAULT_LOCALE):
-        return gettext(msgid)
+    try:
+        with flask_babel.force_locale(_app_config.BABEL_DEFAULT_LOCALE):
+            return gettext(msgid)
+    except Exception as e:
+        logger.error("Failed to force locale: %s", str(e))
+        return gettext(msgid)  # Fallback to current locale
app/models/language.py (3)

Line range hint 32-784: Add documentation for language support flags

Consider adding comments to explain the significance of empty g_tra_code and g_ocr_code values and their impact on functionality.

Add this documentation above the SYSTEM_LANGUAGES_DATA:

"""
Language support matrix:
- g_tra_code: Empty string indicates no Google Translate support
- g_ocr_code: Empty string indicates no Google OCR support
- no_space: True for languages that don't use spaces between words
"""

Line range hint 787-805: Enhance initialization logging

Consider adding more detailed logging to help with debugging and monitoring.

     if cls.objects.count() > 0:
         logger.info(server_gettext("Language collection already initialized."))
         return
     sort = 0
     for lang in cls.SYSTEM_LANGUAGES_DATA:
+        logger.debug("Initializing language: %s (%s)", lang["en_name"], lang["code"])
         cls(
             en_name=lang["en_name"],
             lo_name=lang["lo_name"],
             no_space=lang.get("no_space", False),
             code=lang["code"],
             g_tra_code=lang["g_tra_code"],
             g_ocr_code=lang["g_ocr_code"],
             sort=sort,
         ).save()
         sort += 1
     logger.debug(server_gettext("Initialized Language collection with %d languages"), sort)

910-912: Improve API documentation

The comments explain the behavior but could be formatted as proper documentation.

-            # in non-zh locales gettext(hardcoded_id) should return localized name
-            # otherwise the hardcoded_id is just in zh
-            "lo_name": gettext(self.lo_name) or self.lo_name,
+            # Attempts to get the localized name in the current locale.
+            # Falls back to the original (Chinese) name if no translation exists.
+            "lo_name": gettext(self.lo_name) or self.lo_name,
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9c3ea48 and 98c4ed9.

📒 Files selected for processing (2)
  • app/models/language.py (6 hunks)
  • app/translations/__init__.py (2 hunks)
🔇 Additional comments (1)
app/models/language.py (1)

12-18: LGTM! Well-structured type definition

The LanguageData TypedDict provides good type safety and clear structure for language data.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
app/models/language.py (2)

913-915: Remove redundant or in lo_name assignment

In the to_api method, the use of or self.lo_name is unnecessary because gettext(self.lo_name) will return the original string if no translation is found. Removing the or clause simplifies the code without changing its behavior.

Simplify the code as follows:

-"lo_name": gettext(self.lo_name) or self.lo_name,
+"lo_name": gettext(self.lo_name),

806-808: Adjust logging level for initialization message

The log message indicating the initialization of the language collection is important for tracking application setup. Using logger.debug may result in this message not appearing in production logs. Consider changing the logging level to info to ensure it's recorded appropriately.

Apply this change:

-logger.debug(
+logger.info(
     server_gettext("Initialized Language collection with %d languages"), sort
 )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 98c4ed9 and 36106ff.

📒 Files selected for processing (3)
  • app/models/language.py (6 hunks)
  • app/translations/en/LC_MESSAGES/messages.po (5 hunks)
  • app/translations/zh/LC_MESSAGES/messages.po (4 hunks)
🔇 Additional comments (3)
app/translations/en/LC_MESSAGES/messages.po (1)

1717-1729: Ensure consistent punctuation in translations

Previously, there was an inconsistency in using periods at the end of translations. It appears that this issue has been addressed, and the translations now maintain consistent punctuation.

app/translations/zh/LC_MESSAGES/messages.po (2)

1146-1149: Review and confirm fuzzy translations

Previously, translations for "弗里斯兰语" (Frisian) and "印度尼西亚语" (Indonesian) were marked as fuzzy and required review. Please ensure these translations are accurate and remove any fuzzy markers if they are correct.

To verify, please check the following entries:

 #: app/models/language.py:235
 msgid "弗里斯兰语"
-msgstr ""
+msgstr "弗里斯兰语"

 #: app/models/language.py:341
 msgid "印度尼西亚语"
-msgstr ""
+msgstr "印度尼西亚语"

Also applies to: 1178-1181


1011-1444: ⚠️ Potential issue

Add missing Chinese translations for new language entries

Several new language entries have been added with empty msgstr values. These entries require Chinese translations to ensure proper localization. This includes languages such as "日语" (Japanese), "英语" (English), "韩语" (Korean), and many others.

Please provide the Chinese translations for these entries. For example:

 msgid "日语"
-msgstr ""
+msgstr "日语"

 msgid "英语"
-msgstr ""
+msgstr "英语"

 msgid "韩语"
-msgstr ""
+msgstr "韩语"

Likely invalid or redundant comment.

Comment on lines +50 to 52
"lo_name": hardcode_text("韩语") or hardcode_text("朝鲜语(韩语)"),
"code": "ko",
"g_tra_code": "ko",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Simplify 'lo_name' assignments by removing redundant 'or' operators

The use of the or operator between two hardcode_text function calls in the lo_name assignments may not produce the intended effect. Since hardcode_text returns a non-empty string, the expression after the or will never be evaluated. If the goal is to provide a fallback or alternative, consider using a list or handling this logic elsewhere.

Apply the following changes to simplify the assignments:

-"lo_name": hardcode_text("韩语") or hardcode_text("朝鲜语(韩语)"),
+"lo_name": hardcode_text("韩语"),

-"lo_name": hardcode_text("马拉地语") or hardcode_text("马拉语"),
+"lo_name": hardcode_text("马拉地语"),

-"lo_name": hardcode_text("齐切瓦语 (尼扬贾语)") or hardcode_text("齐切瓦语"),
+"lo_name": hardcode_text("齐切瓦语 (尼扬贾语)"),

-"lo_name": hardcode_text("斯洛文尼亚语") or hardcode_text("斯卡洛文语"),
+"lo_name": hardcode_text("斯洛文尼亚语"),

Also applies to: 485-486, 521-522, 641-642

@@ -888,7 +900,7 @@

def clear(self) -> None:
"""禁止删除语言"""
pass
raise AssertionError(gettext("Language can not be deleted"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use a more appropriate exception than AssertionError

Raising an AssertionError to prevent deletion is not ideal, as assertions are generally used for debugging purposes and may be ignored when optimizations are applied. Instead, consider raising a custom exception or a more suitable built-in exception like NotImplementedError to indicate that the deletion operation is not allowed.

Apply this change:

-def clear(self) -> None:
-    raise AssertionError(gettext("Language can not be deleted"))
+def clear(self) -> None:
+    raise NotImplementedError(gettext("Language cannot be deleted"))

Committable suggestion skipped: line range outside the PR's diff.

@jokester jokester merged commit 08817ca into moeflow-com:main Dec 15, 2024
6 of 7 checks passed
@jokester jokester deleted the more-server-i18n branch December 15, 2024 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant