Skip to content

[Enhancement] L10n 每次调用都重新构造 Set 和 Locale 数组,应 lazy static 缓存 #10

@ShadyUnderLight

Description

@ShadyUnderLight

问题描述

L10n.localizationCandidates(for:) 每次 string() / format() 调用都重新创建 Set<String> from moduleBundle.localizations 和完整的 Locale.preferredLanguages 数组。Localization 在 app 生命周期内不会变化,这部分计算完全没必要重复。

预期结果

availableLocalizationslocalizationCandidates 结果缓存一次,后续调用直接读取。

建议方案

private static let availableLocalizations: Set<String> = {
    Set(moduleBundle.localizations.map { $0.lowercased() })
}()

private static func localizationCandidates(for language: String) -> [String] {
    // ... 逻辑不变,使用 availableLocalizations
}

同时 Bundle.modulelocalizations 取值也只需要一次。

影响模块

性能优化,不影响功能正确性

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions