未验证 提交 d758c082 编写于 作者: O openharmony_ci 提交者: Gitee

!16797 string同步接口变更

Merge pull request !16797 from zt147369/master
......@@ -2057,6 +2057,47 @@ getStringSync(resId: number): string
}
```
### getStringSync<sup>10+</sup>
getStringSync(resId: number, ...args: Array<string | number>): string
用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:**
| 类型 | 说明 |
| ------ | ---------------------------- |
| string | 资源ID值对应的格式化字符串|
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)
**错误码:**
| 错误码ID | 错误信息 |
| -------- | ----------------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001007 | If the resource obtained by resId formatting error. |
**示例:**
```ts
try {
this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78);
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringSync<sup>9+</sup>
getStringSync(resource: Resource): string
......@@ -2101,6 +2142,52 @@ getStringSync(resource: Resource): string
}
```
### getStringSync<sup>10+</sup>
getStringSync(resource: Resource, ...args: Array<string | number>): string
用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:**
| 类型 | 说明 |
| ------ | ---------------------------- |
| string | resource对象对应的格式化字符串|
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)
**错误码:**
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001 | If the resId invalid. |
| 9001002 | If the resource not found by resId. |
| 9001006 | If the resource re-ref too much. |
| 9001007 | If the resource obtained by resId formatting error. |
**示例:**
```ts
let resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
try {
this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78);
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getStringByNameSync<sup>9+</sup>
getStringByNameSync(resName: string): string
......@@ -2140,6 +2227,47 @@ getStringByNameSync(resName: string): string
}
```
### getStringByNameSync<sup>10+</sup>
getStringByNameSync(resName: string, ...args: Array<string | number>): string
用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称 |
| args | Array<string \| number> | 否 | 格式化字符串资源参数 <br> 支持参数类型:<br /> -%d、%f、%s、%% <br> 说明:%%转译符,转译%<br>举例:%%d格式化后为%d字符串|
**返回值:**
| 类型 | 说明 |
| ------ | ---------------------------- |
| string | 资源名称对应的格式化字符串|
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。
**错误码:**
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003 | If the resName invalid. |
| 9001004 | If the resource not found by resName. |
| 9001006 | If the resource re-ref too much. |
| 9001008 | If the resource obtained by resName formatting error. |
**示例:**
```ts
try {
this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78);
} catch (error) {
console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`)
}
```
### getBoolean<sup>9+</sup>
getBoolean(resId: number): boolean
......
......@@ -114,4 +114,48 @@ the resource re-ref too much.
**处理步骤**
查看资源$引用的地方,去除循环引用的情况。
\ No newline at end of file
查看资源$引用的地方,去除循环引用的情况。
## 9001007 根据当前id获取的资源格式化失败
**错误信息**
The resource obtained by resId formatting error.
**错误描述**
resId获取的字符串资源格式化失败。
**可能原因**
1、参数类型不在支持范围内。
2、参数与占位符个数不等。
3、参数与占位符类型不匹配。
**处理步骤**
查看args参数类型与占位符的个数、类型是否一致。
## 9001008 根据当前名称获取的资源格式化失败
**错误信息**
The resource obtained by resName formatting error.
**错误描述**
resName获取的字符串资源格式化失败。
**可能原因**
1、参数类型不在支持范围内。
2、参数与占位符个数不等。
3、参数与占位符类型不匹配。
**处理步骤**
查看args参数类型与占位符的个数、类型是否一致。
\ No newline at end of file
# 全球化子系统ChangeLog
## cl.resourceManager.1 getStringSync、getStringByNameSync新增接口
相较于之前的版本,在4.0.6.2 版本中,为扩展getStringSync、getStringByNameSync接口字符串匹配功能,新增了接口以及相应的错误码。
| 包名 | 新增接口 |
| --------------- | ---------------------------------------------------- |
| ohos.resourceManager.d.ts | getStringSync(resId: number, ...args: Array<string \| number>): string; |
| ohos.resourceManager.d.ts | getStringSync(resource: Resource, ...args: Array<string \| number>): string; |
| ohos.resourceManager.d.ts | getStringByNameSync(resName: string, ...args: Array<string \| number>): string; |
**变更影响**
之前只能直接获取resources中的string资源的value值,在4.0.6.2 及以上版本中可用通过参数格式化string资源的value值,扩展了格式化字符串的查询能力。
新增错误码:
9001007 根据当前id获取的资源格式化失败
9001008 根据当前名称获取的资源格式化失败
**示例代码**
以调用接口getStringSync为例,变更前只支持示例1,变更后示例1、示例2都支持。
```
示例1:
try {
this.context.resourceManager.getStringSync($r('app.string.test').id);
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
}
示例2:
try {
this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 787, 98.78);
} catch (error) {
console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
}
```
**适配指导**
请参考接口说明:
[新增接口参考](../../../application-dev/reference/apis/js-apis-resource-manager.md)
[新增错误码参考](../../../application-dev/reference/errorcodes/errorcode-resource-manager.md)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册