Skip to content

fix: fix wired connection name showing English on Chinese locale#552

Merged
caixr23 merged 1 commit into
linuxdeepin:masterfrom
caixr23:bug-359529
May 16, 2026
Merged

fix: fix wired connection name showing English on Chinese locale#552
caixr23 merged 1 commit into
linuxdeepin:masterfrom
caixr23:bug-359529

Conversation

@caixr23
Copy link
Copy Markdown
Contributor

@caixr23 caixr23 commented May 16, 2026

  1. Add installSystemTranslator() to read system locale from /etc/locale.conf or /etc/deepin-installer/deepin-installer.conf when user account locale is unavailable via D-Bus
  2. Add fallback in addFirstConnection(WiredDevice*) to install system translator when m_initilized is false, covering the onWiredDevicePropertyChanged signal path that bypasses the m_initilized guard in onInitDeviceConnection/onDeviceAdded
  3. Add fallback in 3s timer to try system translator before creating connections with default English names
  4. Only install QTranslator when qm file loads successfully
  5. Update D-Bus service names to match v25 interfaces (org.deepin.dde.LockService1, org.deepin.dde.Accounts1)

Log: Wired connection name displays in system language (e.g. "有线连接") instead of default English "Wired Connection" when system locale is Chinese

Influence:

  1. Set system language to Simplified Chinese, reboot and check control center -> Network -> Wired, verify connection name is "有线连接" not "Wired Connection"
  2. Set system language to other locales, verify wired connection name displays in the corresponding language

fix: 修复系统语言为中文时有线网络名称显示为英文的问题

  1. 新增 installSystemTranslator() 从 /etc/locale.conf 或 /etc/deepin-installer/deepin-installer.conf 读取系统语言, 在无法通过 D-Bus 获取用户账户语言时作为回退
  2. 在 addFirstConnection(WiredDevice*) 中增加回退逻辑, 当 m_initilized 为 false 时安装系统翻译器,覆盖 onWiredDevicePropertyChanged 信号路径绕过 onInitDeviceConnection/onDeviceAdded 中 m_initilized 守卫的情况
  3. 在 3 秒定时器中增加系统翻译器回退,在以默认英文 创建连接之前尝试安装系统语言
  4. 仅在 qm 文件加载成功时才安装 QTranslator
  5. 更新 D-Bus 服务名以匹配 v25 接口 (org.deepin.dde.LockService1, org.deepin.dde.Accounts1)

Log: 有线网络名称将显示为系统语言(如"有线连接")而非
默认英文 "Wired Connection"

Influence:

  1. 系统语言设置为简体中文,重启后进入控制中心-网络-有线网络, 验证连接名称为"有线连接"而非"Wired Connection"
  2. 切换为其他语言,验证有线网络名称显示对应语言

PMS: BUG-359529

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.

Sorry @caixr23, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@caixr23 caixr23 force-pushed the bug-359529 branch 2 times, most recently from 2075947 to 135ecf9 Compare May 16, 2026 05:19
1. Add installSystemTranslator() to read system locale from
   /etc/locale.conf or /etc/deepin-installer/deepin-installer.conf
   when user account locale is unavailable via D-Bus
2. Add fallback in addFirstConnection(WiredDevice*) to install
   system translator when m_initilized is false, covering the
   onWiredDevicePropertyChanged signal path that bypasses the
   m_initilized guard in onInitDeviceConnection/onDeviceAdded
3. Add fallback in 3s timer to try system translator before
   creating connections with default English names
4. Only install QTranslator when qm file loads successfully
5. Update D-Bus service names to match v25 interfaces
   (org.deepin.dde.LockService1, org.deepin.dde.Accounts1)

Log: Wired connection name displays in system language (e.g.
"有线连接") instead of default English "Wired Connection"
when system locale is Chinese

Influence:
1. Set system language to Simplified Chinese, reboot and check
   control center -> Network -> Wired, verify connection name
   is "有线连接" not "Wired Connection"
2. Set system language to other locales, verify wired connection
   name displays in the corresponding language

fix: 修复系统语言为中文时有线网络名称显示为英文的问题

1. 新增 installSystemTranslator() 从 /etc/locale.conf 或
   /etc/deepin-installer/deepin-installer.conf 读取系统语言,
   在无法通过 D-Bus 获取用户账户语言时作为回退
2. 在 addFirstConnection(WiredDevice*) 中增加回退逻辑,
   当 m_initilized 为 false 时安装系统翻译器,覆盖
   onWiredDevicePropertyChanged 信号路径绕过
   onInitDeviceConnection/onDeviceAdded 中 m_initilized
   守卫的情况
3. 在 3 秒定时器中增加系统翻译器回退,在以默认英文
   创建连接之前尝试安装系统语言
4. 仅在 qm 文件加载成功时才安装 QTranslator
5. 更新 D-Bus 服务名以匹配 v25 接口
   (org.deepin.dde.LockService1, org.deepin.dde.Accounts1)

Log: 有线网络名称将显示为系统语言(如"有线连接")而非
默认英文 "Wired Connection"

Influence:
1. 系统语言设置为简体中文,重启后进入控制中心-网络-有线网络,
   验证连接名称为"有线连接"而非"Wired Connection"
2. 切换为其他语言,验证有线网络名称显示对应语言

PMS: BUG-359529
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeeX。我已仔细审查了你提供的Git Diff。本次修改主要涉及:重构拼写错误(initilized -> initializedLaunguage -> Language)、更新D-Bus服务命名规范(com.deepin -> org.deepin)、优化头文件包含顺序、增强翻译文件加载的容错性,以及引入系统级语言回退机制。

整体来看,代码质量有了显著提升,逻辑更加严谨。但仍有一些在语法逻辑、代码质量、性能和安全方面可以进一步优化的地方。以下是详细的审查意见:

1. 语法与逻辑

  • getLocaleValue 函数中的 keywords 参数逻辑冗余/矛盾
    在新增的 getLocaleValue 静态函数中,你在循环内使用了 if (!keywords.isEmpty() && !line.contains(keywords)) continue; 来过滤行。但在 installSystemTranslator 调用时,传入的 keys{"LANGUAGE", "LANG"},而 keywords"LANG"
    问题:如果配置文件中有一行 LANGUAGE=zh_CN.UTF-8,由于它包含 "LANG" 字符串,不会被过滤;但如果有一行 LANG=en_US.UTF-8,也包含 "LANG",也不会被过滤。然而,如果文件中有其他无关行(如 SUPPORTED=zh_CN)恰好包含 "LANG",也会被错误地读入。更严重的是,keys 列表本身已经指明了要匹配哪些键,再用子字符串 contains 去做前置过滤不仅逻辑重复,而且容易误伤或漏判。
    建议:移除 keywords 参数,直接通过 keys 进行精确匹配。

    static QString getLocaleValue(const QString &filePath, const QStringList &keys, const QString &splitKey = "=")
    {
        // ...
        QMap<QString, QString> localeMap;
        QTextStream in(&file);
        while (!in.atEnd()) {
            QString line = in.readLine();
            if (line.trimmed().startsWith('#')) // 顺便过滤注释
                continue;
            QStringList pair = line.split(splitKey, Qt::SkipEmptyParts);
            if (pair.size() >= 2) // 修改为 >= 2,因为值可能包含 '='
                localeMap.insert(pair.at(0).trimmed(), pair.mid(1).join(splitKey).trimmed());
        }
        for (const auto &key : keys) {
            if (localeMap.contains(key))
                return localeMap.value(key).section('.', 0, 0);
        }
        return QString();
    }
  • split 截断导致值丢失的风险
    pair = line.split(splitKey, Qt::SkipEmptyParts) 中,如果 splitKey"=",而配置行是 LANG=zh_CN.UTF-8,此时 pair 大小为2,没问题。但如果行内出现多个 =(如 LANG=A=B),或者值本身含有 =pair.size() == 2 的强校验会导致该行被丢弃,pair.mid(1).join(splitKey) 能更好地保留完整的值。

  • ensureTranslatorInstalled 中状态覆盖的潜在问题

    m_initialized = installSystemTranslator();

    如果 installSystemTranslator() 找不到系统配置文件返回 falsem_initialized 会被赋值为 false。虽然外层有 !m_initialized 保护,但直接用可能返回 false 的值覆盖成员变量,容易在后续迭代中引发逻辑漏洞。
    建议:仅在成功安装时更新状态。

    if (!m_initialized) {
        qCDebug(DSM) << "check connection status";
        checkAccountStatus();
        if (!m_initialized) {
            qCWarning(DSM) << "can not found current user, used default language to create connection";
            if (installSystemTranslator()) {
                m_initialized = true;
            }
        }
    }

2. 代码性能

  • 频繁读取本地文件(I/O 操作在主线程/关键路径)
    新增的 getLocaleValue 每次调用都会打开并读取 /etc/locale.conf 等文件。在 addFirstConnection 中调用了 ensureTranslatorInstalled(),而有线设备的添加和连接是高频事件(特别是系统启动或网络波动时)。
    建议:系统语言在运行期间极少发生变化,可以将系统语言缓存起来,或者只在首次调用时读取文件。

    // 在类中增加缓存变量
    QString m_cachedSystemLocale;
    
    bool NetworkInitialization::installSystemTranslator()
    {
        if (!m_cachedSystemLocale.isEmpty()) {
            installLanguage(m_cachedSystemLocale);
            return true;
        }
        QString locale = getLocaleValue("/etc/locale.conf", {"LANGUAGE", "LANG"});
        // ...
        if (!locale.isEmpty()) {
            m_cachedSystemLocale = locale; // 缓存
            installLanguage(locale);
            return true;
        }
        return false;
    }
  • QTextStream 的编码问题
    Linux 下的 /etc/locale.conf 通常是 UTF-8 编码。QTextStream 默认使用 QTextCodec::codecForLocale(),在某些极端环境下可能不是 UTF-8。
    建议:显式设置编码。

    QTextStream in(&file);
    in.setCodec("UTF-8"); // Qt5
    // 或 in.setEncoding(QStringConverter::Utf8); // Qt6

3. 代码安全

  • 硬编码的系统文件路径缺乏校验
    代码中硬编码了 "/etc/locale.conf""/etc/deepin-installer/deepin-installer.conf"。虽然 /etc 下的文件通常受系统保护,但作为守护进程,读取外部文件仍需谨慎。
    建议
    1. 检查文件的权限和所有者,防止符号链接攻击(Symlink Attack)。在 QFile::open 之前,可以使用 QFileInfo 检查是否为符号链接(isSymLink())及权限。
    2. 对从文件中读取的 locale 字符串进行合法性校验,防止路径注入或异常字符导致后续翻译文件加载出错(如 ../../etc/passwd 这种异常值,虽然 installLanguage 拼接了固定目录,但依然建议校验格式)。
    // 简单的正则校验,确保 locale 格式合法(如 en_US, zh_CN等)
    static bool isValidLocale(const QString &locale) {
        QRegularExpression re("^[a-zA-Z]{2,3}(_[a-zA-Z]{2,3})?(@[a-zA-Z0-9]+)?$");
        return re.match(locale).hasMatch();
    }

4. 代码质量与可读性

  • 拼写纠正的一致性
    本次修改纠正了 initilized -> initializedLaunguage -> LanguageLOCKERVICE -> LOCKSERVICEDEAMON -> DAEMON,这非常棒!但请注意日志和注释中遗留的拼写错误:

    • qCDebug(DSM) << service << "register, initilized" << m_initialized; 中的 "initilized" 应该一并修改为 "initialized"
    • qCWarning(DSM) << "can not found current user, used default language to create connection";used 应为 use(使用动词原形),found 应为 find(情态动词后接原形)。建议改为:"cannot find current user, using default language to create connection"
  • m_untranslactionConnections 命名优化
    类内部成员变量 m_untranslactionConnections 中的 translaction 似乎是 translation 的拼写错误。建议在后续迭代中将其重构为 m_untranslatedConnections

  • 宏定义的命名规范
    D-Bus 路径和接口通常全大写,目前修改为 LOCKSERVICELOCKPATHLOCKINTERFACE 是合理的。但 DAEMONACCOUNTSERVICE 等宏由于单词间没有下划线分隔,可读性略差。建议:DAEMON_ACCOUNT_SERVICEDAEMON_ACCOUNT_PATHDAEMON_ACCOUNT_INTERFACE

总结

你的改进使得网络初始化模块在处理多语言和用户切换时更加健壮,尤其是引入了系统级语言回退机制,解决了用户服务未启动时无语言环境的问题。针对上述意见,优先建议修复 getLocaleValue 的逻辑冗余和 m_initialized 被意外覆盖为 false 的问题,并考虑引入系统语言缓存以提升性能

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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 7c86100 into linuxdeepin:master May 16, 2026
17 of 19 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