Skip to content

fix: translation not loaded if language code not fully matched#351

Merged
BLumia merged 1 commit intolinuxdeepin:masterfrom
BLumia:qtranslator-load
Jun 3, 2025
Merged

fix: translation not loaded if language code not fully matched#351
BLumia merged 1 commit intolinuxdeepin:masterfrom
BLumia:qtranslator-load

Conversation

@BLumia
Copy link
Copy Markdown
Member

@BLumia BLumia commented Jun 3, 2025

修正当系统语言代码与翻译资源的语言代码不完全匹配时(例如资源文件的语言代码中不包括地区代码部分),翻译资源不会被加载的问题.

Summary by Sourcery

Improve translation loading across plugins to handle partial locale codes and provide logging on success or failure.

Bug Fixes:

  • Ensure translation files load even when locale codes don’t exactly match resource filenames.

Enhancements:

  • Standardize use of QTranslator::load(QLocale, baseName, separator, directory) in multiple modules.
  • Add informational and warning logs for translation load success and failure.
  • Clean up translator instances on load failure to prevent invalid installs.

修正当系统语言代码与翻译资源的语言代码不完全匹配时(例如资源文件的语
言代码中不包括地区代码部分),翻译资源不会被加载的问题.

Log:
@BLumia BLumia requested review from ComixHe and caixr23 June 3, 2025 02:13
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

代码审查意见:

  1. 代码重复

    • dock-hotspot-plugin/hotspotplugin.cppdock-network-plugin/networkplugin.cpp中,加载翻译文件的代码逻辑是相同的,可以考虑提取成一个公共函数来减少代码重复。
  2. 错误处理

    • dss-network-plugin/networkmodule.cppnetwork-service-plugin/system/networkinitialization.cpp中,加载翻译文件失败时没有进行错误处理或日志记录,建议添加错误处理逻辑,以便在加载失败时能够及时发现并解决问题。
  3. 资源管理

    • network-service-plugin/system/networkinitialization.cpp中,当加载翻译文件失败时,应该释放已经分配的资源,避免内存泄漏。
  4. 日志级别

    • network-service-plugin/system/networkinitialization.cpp中,使用qCInfo记录加载成功的信息,建议使用qCInfo来记录信息级别的日志,而qCWarning用于记录警告级别的日志。
  5. 代码风格

    • network-service-plugin/system/networkinitialization.cpp中,qmFile变量被定义但未使用,应该移除未使用的变量。
  6. 代码可读性

    • network-service-plugin/system/networkinitialization.cpp中,localTmp变量被定义但未使用,应该移除未使用的变量,以提高代码的可读性。
  7. 翻译文件路径

    • dock-hotspot-plugin/hotspotplugin.cppdock-network-plugin/networkplugin.cpp中,翻译文件的路径是硬编码的,建议使用配置文件或环境变量来管理这些路径,以便于维护和修改。
  8. 错误处理

    • src/networkcontroller.cpp中,当加载翻译文件失败时,应该释放已经分配的资源,避免内存泄漏。
  9. 日志级别

    • src/networkcontroller.cpp中,使用qInfo记录加载成功的信息,建议使用qInfo来记录信息级别的日志,而qWarning用于记录警告级别的日志。
  10. 代码风格

    • src/networkcontroller.cpp中,localeName变量被定义但未使用,应该移除未使用的变量,以提高代码的可读性。

综上所述,建议对代码进行重构,提取公共函数,增加错误处理和日志记录,移除未使用的变量,以及使用配置文件或环境变量来管理路径,以提高代码质量、可维护性和可读性。

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jun 3, 2025

Reviewer's Guide

This PR refactors translation loading across multiple plugins to use QTranslator::load(QLocale, ...) for better locale matching, adds conditional installation based on load success, introduces logging for success/failure, and handles cleanup when loading fails.

Class Diagram of Modified Classes for Translation Handling

classDiagram
    class NetworkInitialization {
        +installUserTranslator(const QString &json): bool
    }
    class NetworkController {
        -m_translator: QTranslator*
        +installTranslator(const QString &locale): void
    }
    class HotspotPlugin {
        +HotspotPlugin(QObject *parent)
    }
    class NetworkPlugin {
        +NetworkPlugin(QObject *parent)
    }
    class NetworkModule {
        +installTranslator(const QString &locale): void
    }

    note "The methods/constructors listed are updated to use QTranslator::load(QLocale, ...) for improved locale matching, conditional installation, and logging."
Loading

File-Level Changes

Change Details Files
Use QTranslator.load overload for flexible locale matching and conditional installation
  • Replaced manual .qm file path construction with QTranslator.load(QLocale(...), baseName, sep, directory)
  • Wrapped load calls in if (...) blocks to check success before installing
  • Added qInfo/qWarning (or qCInfo/qCWarning) statements to log success or failure
  • On failure in NetworkController, deleted translator and cleared pointer
networkinitialization.cpp
networkcontroller.cpp
hotspotplugin.cpp
networkplugin.cpp
networkmodule.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: BLumia, caixr23

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @BLumia - I've reviewed your changes - here's some feedback:

  • Consider refactoring the repeated QTranslator::load + installTranslator logic into a shared helper to avoid duplicating the same code across modules.
  • Make sure you consistently remove or delete any existing QTranslator before installing a new one (and on failure) to prevent memory leaks or duplicate translations.
  • Standardize on one logging API (e.g. qCInfo/qCWarning with proper categories) instead of mixing qInfo/qWarning and qCInfo/qCWarning for more consistent diagnostics.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@BLumia BLumia merged commit 9f8ae1d into linuxdeepin:master Jun 3, 2025
15 of 18 checks passed
@BLumia BLumia deleted the qtranslator-load branch June 3, 2025 02:37
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.

3 participants