Skip to content

SOUI 字符串资源系统指南

Warning

The current page still doesn't have a translation for this language.

You can read it through google translate.

字符串是UI交互的基础元素,SOUI提供了完整的字符串资源管理系统,支持多语言、动态更新和统一管理。

字符串资源概述

字符串资源系统具有以下核心特性: - 统一管理:集中定义所有UI文本内容 - 多语言支持:轻松实现国际化和本地化 - 动态更新:运行时修改字符串内容 - 代码分离:将文本内容与界面布局解耦

字符串资源定义

创建字符串资源文件

在项目的values目录下创建字符串资源文件,推荐命名为strings.xml

<?xml version="1.0" encoding="utf-8"?>
<strings>
    <!-- 应用标题 -->
    <app_name value="SOUI应用"/>

    <!-- 通用操作 -->
    <action_ok value="确定"/>
    <action_cancel value="取消"/>
    <action_save value="保存"/>
    <action_delete value="删除"/>

    <!-- 菜单项 -->
    <menu_file value="文件"/>
    <menu_edit value="编辑"/>
    <menu_help value="帮助"/>

    <!-- 提示信息 -->
    <msg_welcome value="欢迎使用SOUI框架"/>
    <msg_loading value="加载中..."/>
    <msg_error value="操作失败,请重试"/>

    <!-- 输入提示 -->
    <hint_username value="请输入用户名"/>
    <hint_password value="请输入密码"/>
    <hint_search value="搜索内容"/>
</strings>

命名规范建议

命名前缀 用途示例 说明
app_ app_name, app_version 应用级字符串
action_ action_save, action_delete 操作按钮文本
menu_ menu_file, menu_edit 菜单项文本
msg_ msg_success, msg_error 提示消息
hint_ hint_username, hint_search 输入框提示
title_ title_settings, title_about 页面/对话框标题
desc_ desc_feature, desc_privacy 描述性文本

资源配置与注册

添加到资源索引

uires.idx中注册字符串资源文件:

<resources>
    <values>
        <file name="strings" path="values\strings.xml"/>
    </values>
</resources>

在应用配置中引用

init.xml中添加字符串资源引用:

<UIDEF>
    <resources>
        <string src="values:strings"/>
    </resources>
</UIDEF>

使用字符串资源

在布局文件中使用

通过@string/语法引用定义的字符串资源:

<!-- 窗口标题 -->
<window name="main_wnd" title="@string/app_name" width="800" height="600">
    <!-- 登录表单 -->
    <window name="login_panel" layout="vbox" gravity="center" padding="20">
        <text text="@string/title_login" size="18" colorText="#333333" marginBottom="20"/>

        <edit name="username" width="200" height="36" 
              text="" hint="@string/hint_username" marginBottom="10"/>

        <edit name="password" width="200" height="36" 
              text="" hint="@string/hint_password" password="true" marginBottom="20"/>

        <window layout="hbox" gravity="center">
            <button name="btn_login" text="@string/action_login" width="100" height="36"/>
            <button name="btn_cancel" text="@string/action_cancel" width="100" height="36" marginLeft="10"/>
        </window>
    </window>
</window>

在控件属性中使用

字符串资源可用于各种文本相关的控件属性:

<!-- 按钮文本 -->
<button text="@string/action_save" width="80" height="32"/>

<!-- 文本控件 -->
<text text="@string/msg_welcome" size="16" colorText="#333333"/>

<!-- 输入框提示 -->
<edit hint="@string/hint_search" width="200" height="30"/>

<!-- 菜单项 -->
<menu_item text="@string/menu_file" id="1001"/>

在代码中使用

获取字符串值

// 获取字符串资源
SStringT strWelcome = GETSTRING("@string/msg_welcome");
SStringT strButtonText = GETSTRING("@string/action_ok");

// 使用字符串设置控件文本
pWnd->SetWindowText(GETSTRING("@string/title_main"));

使用命名ID访问(推荐)

如果启用了XmlNamedID功能,可以使用更安全的命名ID方式:

// 在xml中定义字符串时添加id属性
// <string id="welcome_message" value="欢迎使用"/>

// 在代码中使用命名ID访问
SStringT strWelcome = GETSTRING(R::string::welcome_message);

高级用法

字符串格式化

支持在字符串中使用占位符:

<string name="format_user_greeting" value="欢迎回来,{0}!您有{1}条未读消息。"/>
SStringT strGreeting = SStringT().Format(GETSTRING("@string/format_user_greeting"), 
                                        strUserName, nUnreadCount);

最佳实践

设计原则

  1. 语义化命名:使用描述性的名称而非具体文本内容
  2. 模块化组织:按功能模块分组字符串
  3. 避免硬编码:所有用户可见文本都使用字符串资源
  4. 保持一致性:相同含义的文本使用相同的字符串资源

维护建议

  1. 定期审查:检查未使用的字符串资源
  2. 版本控制:使用版本号管理字符串变更
  3. 文档化:为复杂字符串添加注释说明
  4. 测试覆盖:确保所有语言版本的完整性

常见问题解答

Q: 如何处理字符串中的特殊字符? A: 使用XML实体编码,如&amp;表示&&lt;表示<等。

Q: 如何调试字符串资源问题? A: 启用SOUI的日志功能,可以查看字符串加载和解析的详细信息。