HarmonyOS Next快速入门:类型定义和国际化

##HarmonyOS Next快速入门##HarmonyOS应用开发##教育##

点击跳转《HarmonyOS Next快速入门》视频教程

Resource 资源引用类型,用于设置组件属性的值。

可以通过r或者rawfile创建Resource类型对象,不可以修改Resource中的各属性的值。

typescript 复制代码
$r('belonging.type.name')
//belonging:系统资源或者应用资源,相应的取值为'sys'和'app';
//type:资源类型,支持'string'、'color'、'boolean'、'float'、'intarray'、'integer'、'pattern'、'plural'、'strarray'、'media';
//name:资源名称,在资源定义时确定。

$rawfile('filename')
//filename:工程中resources/rawfile目录下的文件名称。

推荐大家优先使用Resource类型,将资源文件(字符串、图片、音频等)统一存放于resources目录下,便于开发者统一维护。同时系统可以根据当前配置加载合适的资源,例如,开发者可以根据屏幕尺寸呈现不同的布局效果,或根据语言设置提供不同的字符串。

颜色代码参考:

typescript 复制代码
https://www.runoob.com/cssref/css-colornames.html

代码实例:ResourcePage

typescript 复制代码
@Entry
@Component
struct ResourcePage {
  @State message: string = 'Resource';

  build() {
    Column() {
      Text(this.message)
        .fontSize(30)
        .fontWeight(FontWeight.Bold)

      Button($r('app.string.label_login')).backgroundColor($r('app.color.btn_color'))

      Image($rawfile('cat.jpg')).size({width:100})
    }
    .height('100%')
    .width('100%')
  }
}

国际化@ohos.i18n 本模块提供系统相关的或者增强的国际化能力,包括区域管理、电话号码处理、日历等,相关接口为ECMA 402标准中未定义的补充接口。Intl模块提供了ECMA 402标准定义的基础国际化接口,与本模块共同使用可提供完整地国际化支持能力。

导入模块

typescript 复制代码
import { i18n } from '@kit.LocalizationKit';

文本按指定国家进行本地化显示。

typescript 复制代码
static getDisplayCountry(country: string, locale: string, sentenceCase?: boolean): string

代码实例

typescript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';

try {
    let displayCountry: string = i18n.System.getDisplayCountry("zh-CN", "en-GB"); // displayCountry = "China"
} catch (error) {
    let err: BusinessError = error as BusinessError;
    console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`);
}

文本按指定语言进行本地化显示。

typescript 复制代码
static getDisplayLanguage(language: string, locale: string, sentenceCase?: boolean): string

代码示例

typescript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';

try {
  let displayLanguage: string = i18n.System.getDisplayLanguage("zh", "en-GB"); // 用英文形式显示中文,displayLanguage = Chinese
} catch(error) {
  let err: BusinessError = error as BusinessError;
  console.error(`call System.getDisplayLanguage failed, error code: ${err.code}, message: ${err.message}.`);
}

针对输入语言,系统支持的国家或地区列表。

typescript 复制代码
static getSystemCountries(language: string): Array<string>

代码实例

typescript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';

try {
  let systemCountries: Array<string> = i18n.System.getSystemCountries('zh'); // systemCountries = [ "ZW", "YT", "YE", ..., "ER", "CN", "DE" ]
} catch(error) {
  let err: BusinessError = error as BusinessError;
  console.error(`call System.getSystemCountries failed, error code: ${err.code}, message: ${err.message}.`);
}

判断当前语言和地区是否匹配。

typescript 复制代码
static isSuggested(language: string, region?: string): boolean

代码实例

typescript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';

try {
  let res: boolean = i18n.System.isSuggested('zh', 'CN');  // res = true
} catch(error) {
  let err: BusinessError = error as BusinessError;
  console.error(`call System.isSuggested failed, error code: ${err.code}, message: ${err.message}.`);
}

设置应用的偏好语言。设置偏好语言为"default"后,应用语言将跟随系统语言,应用冷启动生效。

typescript 复制代码
static setAppPreferredLanguage(language: string): void

代码实例

typescript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';

try {
  i18n.System.setAppPreferredLanguage('zh'); // 设置应用当前的偏好语言为 "zh"
} catch(error) {
  let err: BusinessError = error as BusinessError;
  console.error(`call System.setAppPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.`);
}
相关推荐
不凡的凡6 小时前
鸿蒙图片相似性对比
华为·harmonyos
Georgewu8 小时前
【HarmonyOS】HAR和HSP循环依赖和依赖传递问题详解
harmonyos
Georgewu9 天前
【HarmonyOS 5】鸿蒙跨平台开发方案详解(一)
flutter·harmonyos
万少10 天前
重磅推出 🔥 HarmonyOS AI 助手 CodeGenie V6 的使用教程
前端·harmonyos
我睡醒再说10 天前
纯血HarmonyOS5 打造小游戏实践:绘画板(附源文件)
harmonyos
我睡醒再说10 天前
HarmonyOS 5 ArkTS Worker线程:构建高性能移动应用的并行计算引擎
harmonyos
我睡醒再说10 天前
纯血HarmonyOS5 打造小游戏实践:扫雷(附源文件)
harmonyos
二流小码农10 天前
鸿蒙开发:基于node脚本实现组件化运行
android·ios·harmonyos
Moximxxx10 天前
Harmory Next 下拉刷新 悬浮loading效果
harmonyos