Skip to content

fix: Use the system default language when unable to retrieve users#375

Merged
caixr23 merged 1 commit intolinuxdeepin:masterfrom
caixr23:master
Jul 7, 2025
Merged

fix: Use the system default language when unable to retrieve users#375
caixr23 merged 1 commit intolinuxdeepin:masterfrom
caixr23:master

Conversation

@caixr23
Copy link
Copy Markdown
Contributor

@caixr23 caixr23 commented Jul 7, 2025

Use the system default language when unable to retrieve users

pms: BUG-286715

Summary by Sourcery

Use the system’s default locale as a fallback when user-specific language data is unavailable, and ensure translators are only installed upon successful loading.

Bug Fixes:

  • Fallback to system default language when current user cannot be retrieved

Enhancements:

  • Add installSystemTranslator to parse /etc/locale.conf and apply system locale
  • Extract installTranslator helper and update initConnection to invoke system fallback
  • Guard translation installation to only occur if translator.load succeeds

Use the system default language when unable to retrieve users

pms: BUG-286715
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

代码审查意见:

  1. networkmodule.cpp文件中,installTranslator函数中增加了对translator.load的返回值检查,这是一个好的做法,可以避免加载失败时继续安装翻译器,从而避免潜在的运行时错误。

  2. networkinitialization.cpp文件中,installUserTranslator函数中新增了installTranslator函数,这是一个合理的重构,将安装翻译器的逻辑分离出来,提高了代码的可读性和可维护性。

  3. networkinitialization.cpp文件中,installSystemTranslator函数中读取/etc/locale.conf文件来获取系统语言,这是一个有效的方法来获取系统默认语言。但是,如果/etc/locale.conf文件不存在或者格式不正确,可能会导致函数返回默认值,这可能不是预期的行为。建议增加错误处理逻辑,例如检查文件是否存在以及内容格式是否正确。

  4. networkinitialization.cpp文件中,installSystemTranslator函数中使用了QMap来存储locale.conf文件的内容,这是一个好的选择,因为它提供了方便的键值对操作。但是,如果locale.conf文件中的每一行都包含多个等号,那么split('=')可能会导致错误。建议在解析文件内容时增加适当的错误处理逻辑。

  5. networkinitialization.cpp文件中,installSystemTranslator函数中使用了qCInfo来输出安装的系统语言,这是一个好的做法,可以帮助调试和日志记录。但是,如果这个函数在生产环境中被频繁调用,可能会产生大量的日志输出,建议根据需要调整日志级别。

  6. networkinitialization.cpp文件中,initConnection函数中增加了对m_initilized的检查,这是一个好的做法,可以避免在初始化已经完成的情况下重复执行初始化逻辑。

  7. networkinitialization.cpp文件中,initConnection函数中增加了对installSystemTranslator的调用,这是一个合理的改进,可以确保系统语言被正确安装。

  8. networkinitialization.h文件中,新增了installTranslatorinstallSystemTranslator函数的声明,这是一个好的做法,可以保持接口的一致性。

总体来说,代码的修改和重构提高了代码的可读性、可维护性和健壮性。但是,还需要注意对错误处理和日志记录的适当调整,以确保在生产环境中能够正确运行。

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jul 7, 2025

Reviewer's Guide

This PR implements a fallback mechanism to use the system default locale when user‐specific language data cannot be retrieved by introducing new translator installation functions, updating the initialization flow to invoke them on failure, and refining the translator loading logic in the network plugin.

Sequence diagram for fallback to system default language during initialization

sequenceDiagram
    participant NetworkInitialization
    participant SystemLocale
    participant Translator
    participant Connection

    NetworkInitialization->>NetworkInitialization: checkAccountStatus()
    alt User found
        NetworkInitialization->>Translator: installUserTranslator(userLocale)
    else User not found
        NetworkInitialization->>SystemLocale: installSystemTranslator()
        SystemLocale->>Translator: installTranslator(systemLocale)
    end
    NetworkInitialization->>Connection: addFirstConnection()
Loading

Class diagram for updated NetworkInitialization translator logic

classDiagram
    class NetworkInitialization {
        +bool installUserTranslator(const QString &json)
        +bool installTranslator(const QString &locale)
        +bool installSystemTranslator()
        void initConnection()
    }

    class Translator
    NetworkInitialization --> Translator : uses
Loading

Class diagram for updated NetworkModule translator loading

classDiagram
    class NetworkModule {
        void installTranslator(const QString &locale)
        QString localTmp
        Translator translator
        Manager* m_manager
    }
    class Translator
    class Manager {
        void updateLanguage(QString)
    }
    NetworkModule --> Translator : uses
    NetworkModule --> Manager : updates language
Loading

File-Level Changes

Change Details Files
Invoke system translator fallback in initConnection when user lookup fails
  • Wrap the m_initialized check in initConnection with braces
  • Log a warning and call installSystemTranslator() if no current user found
network-service-plugin/system/networkinitialization.cpp
Add generic translator installation and system locale parsing
  • Declare and implement installTranslator(locale) to centralize translation loading
  • Implement installSystemTranslator() to read /etc/locale.conf, extract LANG or LANGUAGE, and invoke installTranslator
  • Add QFile and QMap includes and update header to expose new methods
network-service-plugin/system/networkinitialization.cpp
network-service-plugin/system/networkinitialization.h
Guard translator installation in DSS network module
  • Wrap translator.load() in an if and only install when load succeeds
  • Ensure m_manager->updateLanguage() still runs after attempted load
dss-network-plugin/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

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 @caixr23 - I've reviewed your changes and they look great!


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.

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

@caixr23 caixr23 merged commit a243fce into linuxdeepin:master Jul 7, 2025
15 of 17 checks passed
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