1# Resource Categories and Access 2 3<!--Kit: Localization Kit--> 4<!--Subsystem: Global--> 5<!--Owner: @liule_123--> 6<!--Designer: @buda_wy--> 7<!--Tester: @lpw_work--> 8<!--Adviser: @Brilliantry_Rui--> 9 10During application development, you may need to use different resources, such as strings, colors, fonts, spacing, and icons. The values of these resources vary with devices or configurations. This topic describes resource types and provides guidance for resource development. 11Resources are classified into the following types based on their sources: 12 13- Application resources: resources customized by developers in applications. You can use resource files to manage resources on different devices or configurations. 14- System resources: resources provided by the system. You can obtain system icon resources from [HarmonyOS Symbol](https://developer.huawei.com/consumer/en/design/harmonyos-symbol/) and system color, spacing, and rounded corner resources from the [system resource layer design table](https://gitcode.com/openharmony/docs/blob/master/en/design/ux-design/design-resources.md). In addition, you can obtain system color resources from [full table of basic and semantic tokens](https://developer.huawei.com/consumer/en/doc/design-guides/color-0000001776857164#section17672143841113). The resources in this table do not overlap with those in the system resource layer design table. Both are recommended system color resources.<br>Other system resources that are not listed in the tables are system component and underlying capability parameters. You are advised to use the system resources provided in the preceding tables. 15 16## Resource Categories 17 18Resource files used during application development must be stored in specified directories for management. There are two types of resource directories, namely, resource directories and resource group directories. The resource directories are the **base**, qualifiers, **rawfile**, and **resfile** directories. The resource group directories are the **element**, **media**, and **profile** directories. 19 20``` 21resources 22|---base // Default directory. 23| |---element 24| | |---string.json // String resource file. 25| |---media 26| | |---icon.png // Media resource file, which can be an image or a video. 27| |---profile 28| | |---test_profile.json // User-defined profile. 29|---en_GB-vertical-car-mdpi // Example of a qualifiers subdirectory, which needs to be created on your own. 30| |---element 31| | |---string.json 32| |---media 33| | |---icon.png 34| |---profile 35| | |---test_profile.json 36|---rawfile // Other types of files are saved as raw files and will not be integrated into the resources.index file. You can customize the file name as needed. 37|---resfile // Other types of files are saved as raw files and will not be integrated into the resources.index file. You can customize the file name as needed. 38``` 39> **NOTE** 40> 41> - Files in resource directories and resource group directories are considered as resource files and will not be obfuscated during application packaging. 42> 43> - The common resource files used across projects in the stage model are stored in the **resources** directory under **AppScope**. 44> 45> - The resource files in the **AppScope** directory are merged into the **resources** directory. If files with the same name exist in these two directories, the ones in the **AppScope** directory are retained after build and packaging. 46 47### Resource Directories 48 49Table 1 Description of resource directories 50 51| Directory| Description| 52|--- | --- | 53| base directory| The **base** directory is provided by default. Under this directory, the **element** subdirectory stores basic elements such as strings, colors, and Boolean values, and the **media** and **profile** subdirectories store resource files such as media, animations, and layouts.<br>Resource files in the directories are compiled into binary files, and each resource file is assigned an ID. Access resource files in the directories based on the resource type and resource name.| 54| Qualifiers directory| The qualifiers directory needs to be created as required. Under this directory, the **element** subdirectory stores basic elements such as strings, colors, and Boolean values, and the **media** and **profile** subdirectories store resource files such as media, animations, and layouts.<br>Similarly, resource files in the directories are compiled into binary files, and each resource file is assigned an ID. Access resource files in the directories based on the resource type and resource name. For details about the meaning and value range of qualifiers and the naming rules of this directory, see [Qualifiers Directory](#qualifiers-directory).| 55| rawfile directory| You can create multiple levels of subdirectories with custom names to store various resource files.<br>Resource files in the sub-directory are directly packed into the application without being compiled, and no IDs will be assigned to the resource files. Access the directories based on the specified file path and file name.| 56| resfile Directory| You can create multiple levels of subdirectories with custom names to store various resource files.<br>Resource files in the sub-directory are directly packed into the application without being compiled, and no IDs will be assigned to the resource files. After the application is installed, resources in the **resfile** repository are decompressed to the application sandbox path. After the **resfile** directory is obtained through the **Context** attribute [resourceDir](../reference/apis-ability-kit/js-apis-inner-application-context.md#properties), the resources can be accessed through the file path in read-only mode.| 57 58### Resource Group Directories 59 60Table 2 Resource group directories 61 62| Directory | Description | Resource File | 63| --------- | ---------------------------------------- | ---------------------------------------- | 64| element | Element resources. Each type of data is represented by a JSON file. (Only files are supported in this directory.) The options are as follows:<br>- **boolean**: boolean data<br>- **color**: color data<br>- **float**: floating point number, ranging from -2^128 to 2^128<br>- **intarray**: array of integers<br>- **integer**: integer, ranging from -2^31 to 2^31-1<br>- **plural**: plural form data<br>- **strarray**: array of strings<br>- **string**: string. For details about how to format strings, see [API Reference](../reference/apis-localization-kit/js-apis-resource-manager.md#getstringsync10).<!--Del--><br>- **pattern**: style (for system applications only)<!--DelEnd--><!--Del--><br>- **theme**: theme (for system applications only)<!--DelEnd-->| It is recommended that files in the **element** subdirectory be named the same as the following files, each of which can contain only data of the same type:<br>- boolean.json<br>- color.json<br>- float.json<br>- intarray.json<br>- integer.json<br>- plural.json<br>- strarray.json<br>- string.json<!--Del--><br>- pattern.json<!--DelEnd--><!--Del--><br>- theme.json<!--DelEnd--> | 65| media | Indicates media resources, including non-text files such as images, audios, and videos. (Only files are supported in this directory.)<br>Table 3 and Table 4 describe the types of images, audios, and videos. | The file name can be customized, for example, **icon.png**. | 66| profile | Indicates a custom configuration file. You can obtain the file content by using the [getProfileByAbility](../reference/apis-ability-kit/js-apis-bundleManager.md#bundlemanagergetprofilebyability) API. (Only JSON files are supported in this directory.) | The file name can be customized, for example, **test_profile.json**. | 67 68**Media Resource Types** 69 70Table 3 Image resource types 71 72| Format | File Name Extension| 73| ---- | ----- | 74| JPEG | .jpg | 75| PNG | .png | 76| GIF | .gif | 77| SVG | .svg | 78| WEBP | .webp | 79| BMP | .bmp | 80 81Table 4 Audio and video resource types 82 83| Format | File Name Extension | 84| ------------------------------------ | --------------- | 85| H.264 AVC |.3gp | 86| Baseline Profile (BP) | .mp4 | 87 88 89### Qualifiers Directory 90 91The name of a qualifiers directory consists of one or more qualifiers that represent the application scenarios or device characteristics. Qualifiers include the mobile country code (MCC), mobile network code (MNC), language, script, country or region, screen orientation, device type, night mode, and screen density. Underscores (\_) or hyphens (\-) are used to separate qualifiers. Before creating a qualifiers directory, familiarize yourself with the following directory naming conventions: 92 93- Qualifiers are ordered in the following sequence: MCC_MNC-language_script_country/region-screen orientation-device type-night mode-screen density. You can select one or multiple qualifiers to name your subdirectory based on your application scenarios and device characteristics. 94 95- Separation between qualifiers: the MCC and MNC qualifiers are separated by underscores (\_); the language, script, and country/region qualifiers are separated by underscores (\_); other qualifiers are separated by hyphens (-). For example, **mcc460_mnc00-zh_Hant_CN** and **zh_CN-car-ldpi**. 96 97- Value range of qualifiers: The value of each qualifier must meet the requirements specified in the following table. Otherwise, the resource files in the resources directory cannot be matched. 98 99Table 5 Requirements for qualifier values 100 101| Qualifier Type | Description and Value Range | 102| ----------- | ---------------------------------------- | 103| MCC&MNC| Indicates the MCC and MNC, which are obtained from the network where the device is registered.<br>The MCC can be either followed by the MNC with an underscore (_) in between or be used independently. For example, **mcc460** represents China, and **mcc460_mnc00** represents China Mobile.<br>For details about the value range, see [ITU-T E.212](https://www.itu.int/rec/T-REC-E.212) (the international identification plan for public networks and subscriptions).| 104| Language | Indicates the language used by the device. The value consists of two or three lowercase letters. For example, **zh** indicates Chinese, **en** indicates English, and **mai** indicates Maithili.<br>For details about the value range, see [ISO 639](https://www.iso.org/iso-639-language-code) (codes for the representation of names of languages).| 105| Text | Indicates the script type used by the device. The value starts with one uppercase letter followed by three lowercase letters. For example, **Hans** indicates simplified Chinese, and **Hant** indicates traditional Chinese.<br>For details about the value range, see [ISO 15924](https://www.iso.org/standard/81905.html) (codes for the representation of names of scripts).| 106| Country/Region | Indicates the country or region where the user is located. The value consists of two or three uppercase letters or three digits. For example, **CN** indicates China, and **GB** indicates the United Kingdom.<br>For details about the value range, see [ISO 3166-1](https://www.iso.org/iso-3166-country-codes.html) (codes for the representation of names of countries and their subdivisions).| 107| Screen orientation | Indicates the screen orientation of the device. The value can be:<br>- **vertical**: portrait orientation<br>- **horizontal**: landscape orientation| 108| Device type | <!--RP1-->Indicates the device type. The value can be:<br>- **car**: a telematics device<br>- **tablet**: tablet<br>- **tv**: smart TV<br>- **wearable**: smart wearable<!--RP1End--> | 109| Color mode | Indicates the color mode of the device. The value can be:<br>- **dark**: dark mode<br>- **light**: light mode| 110| Screen density | Indicates the screen density of the device, in dpi. The value can be:<br>- **sdpi**: small-scale DPI. This value is applicable to devices with a DPI range of (0, 120].<br>- **mdpi**: medium-scale DPI. This value is applicable to devices with a DPI range of (120, 160].<br>- **ldpi**: large-scale DPI. This value is applicable to devices with a DPI range of (160, 240].<br>- **xldpi**: extra-large-scale DPI. This value is applicable to devices with a DPI range of (240, 320].<br>- **xxldpi**: extra-extra-large-scale DPI. This value is applicable to devices with a DPI range of (320, 480].<br>- **xxxldpi**: extra-extra-extra-large-scale DPI. This value is applicable to devices with a DPI range of (480, 640].| 111 112### Resource File Examples 113 114The content of the **color.json** file is as follows: 115 116The standard hexadecimal color value consists of eight hexadecimal digits. The first two digits indicate the opacity and the last six digits indicate the color value. 117```json 118{ 119 "color": [ 120 { 121 "name": "color_hello", 122 "value": "#ffff0000" 123 }, 124 { 125 "name": "color_world", 126 "value": "#ff0000ff" 127 } 128 ] 129} 130``` 131 132The content of the **float.json** file is as follows: 133 134```json 135{ 136 "float": [ 137 { 138 "name": "font_hello", 139 "value": "28.0fp" 140 }, 141 { 142 "name": "font_world", 143 "value": "20.0fp" 144 } 145 ] 146} 147``` 148 149The content of the **string.json** file is as follows: 150 151```json 152{ 153 "string": [ 154 { 155 "name": "string_hello", 156 "value": "Hello" 157 }, 158 { 159 "name": "string_world", 160 "value": "World" 161 }, 162 { 163 "name": "message_arrive", 164 "value": "We will arrive at %1$s." 165 }, 166 { 167 "name": "message_notification", 168 "value": "Hello, %1$s!,You have %2$d new messages." 169 } 170 ] 171} 172``` 173 174The content of the **plural.json** file is as follows: 175 176```json 177{ 178 "plural": [ 179 { 180 "name": "eat_apple", 181 "value": [ 182 { 183 "quantity": "one", 184 "value": "%d apple" 185 }, 186 { 187 "quantity": "other", 188 "value": "%d apples" 189 } 190 ] 191 } 192 ] 193} 194``` 195 196## Creating a Resource Directory and Resource File 197 198You can create a directory and its files under the **resources** directory based on the naming rules of the qualifiers directory and file types supported by resource group directories. DevEco Studio provides a wizard for you to create resource directories and resource files. 199 200### Creating a Resource Directory and Resource File 201 202Right-click the **resources** directory and choose **New** > **Resource File**. By default, the file is created in a resource group directory under **base**. If one or more qualifiers are selected, the system automatically generates a subdirectory and creates the file in this subdirectory. 203 204Qualifiers of different types can be combined. For example, if **Locale** is set to **zh_CN** and **ColorMode** is set to **Dark**, the qualifiers directory name will be **zh_CN-dark**. For details about the combination rules, see [Qualifiers Directory](#qualifiers-directory). 205 206In **File name**, enter the name of the resource file to create. In **Resource type**, select the type of the resource group, which is **element** by default. In **Root Element**, select a resource type. To select a qualifier, highlight it under **Available qualifiers** and click the right arrow. To deselect a qualifier, highlight it under **Selected qualifiers** and click the left arrow.<br>The created directory is automatically named in the format of *Qualifiers.Resource group type*. For example, if you create a directory by setting **Color Mode** to **Dark** and **Resource type** to **Element**, the system automatically generates a directory named **dark/element**. 207 208  209 210### Creating a Resource Directory 211 212Right-click the **resources** directory and choose **New** > **Resource Directory** to create a directory only. By default, the **base** directory is created. If one or more qualifiers are selected, the system automatically generates the qualifiers directories and resource group directories. After specifying the qualifier, select a resource group type. Currently, the supported resource group types are **Element**, **Media**, and **Profile**. A resource directory will be generated upon creation. 213 214  215 216### Creating a Resource File 217 218Right-click a resource group directory (**element**, **media**, or **profile**) and choose **New** > **XXX Resource File**. A resource file under this directory is then created. For example, you can create an element resource file in the **element** subdirectory. 219 220  221 222### Example 223 224The following takes the Chinese and English string resource files as examples to describe how to create resources with different qualifiers. 225 2261. Right-click the **resources** directory and choose **New** > **Resource File** from the shortcut menu. Set **File name** to **string_sample**, **Resource type** to **Element**, **Root Element** to **string**, and **Available qualifiers** to **Locale**. Select **zh** from the language list and **CN** from the country/region list. After all is set, the **zh_CN/element/string_sample.json** file is created in the **resources** directory. 227  228 2292. Similarly, select **en** for language and **US** for country/region to create the **en_US/element/string_sample.json** file. 230 231The following figure shows the created resource files. For details about how to access the created resource files, see [Accessing Resources](#accessing-resources). 232 233 234 235## Using the attr Attribute for Resource Translation 236 237### Function Description 238 239If the string resource referenced by an application needs to support multi-language adaptation, the **attr** attribute can be used to mark the translation scope and status of the string. The **attr** attribute is not involved in resource compilation. 240 241If the **attr** attribute is not configured, a string is translated by default. 242 243**Parameters of attr** 244 245| Name | Type | Description | 246| --------- | ----------------------- | ---- | 247| translatable | boolean | Whether the string needs to be translated.<br>**true**: The string needs to be translated.<br>**false**: The string does not need to be translated. | 248| priority | string | Translation status of the string.<br>**code**: untranslated<br>**translate**: translated but not verified<br>**LT**: translated and verified<br>**customer**: custom | 249 250### Constraints 251The **attr** attribute applies to the string, strarray, and plural resources in the **base** directory. 252``` 253resources 254|---base 255| |---element 256| | |---string.json 257| | |---strarray.json 258| | |---plural.json 259``` 260### Example 261The following shows the **attr** attribute configured in **string**. The **string1** string is marked as not to be translated, and the **string2** string is marked as to be translated and the translation has been verified. 262 263```json 264{ 265 "string": [ 266 { 267 "name": "string1", 268 "value": "1", 269 "attr": { 270 "translatable": false 271 } 272 }, 273 { 274 "name": "string2", 275 "value": "Hello world!", 276 "attr": { 277 "translatable": true, 278 "priority": "LT" 279 } 280 } 281 ] 282} 283``` 284 285## Accessing Resources 286 287### HAP Resources 288 289 - Access resources through **$r()** or **$rawfile()**.<br>Resources of the color, float, string, plural, media and profile types are accessed through **$r('app.type.name')**, in which **app** indicates the resource defined in the **resources** directory, **type** indicates the resource type, and **name** indicates the resource name.<br>To access strings with multiple placeholders in the **string.json** file, for example, **$s** and **$d** in a value, use the **$r('app.string.label', 'aaa', 444)** format, where **label** indicates the resource name, and **'aaa'** and **444** are used to replace placeholders.<br>To access resources in the **rawfile** subdirectory, use the **$rawfile('filename')** format. Wherein **filename** indicates the relative path of a file in the **rawfile** subdirectory, which must contain the file name extension and cannot start with a slash (/). 290 291 > **NOTE** 292 > 293 > For details about how to use native APIs to access raw files, see [Raw File Development](../napi/rawfile-guidelines.md). 294 295[Resource file examples](#resource-file-examples) show different .json files, including **color.json**, **string.json**, and **plural.json**. Before accessing application resources, you need to learn the usage specifications of the .json files.<br>The usage is as follows: 296 297 ```ts 298 // Access through $r('app.type.name'). 299 Text($r('app.string.string_hello')) 300 .fontColor($r('app.color.color_emphasize')) 301 .fontSize($r('app.float.text_size_headline1')) 302 .fontFamily($r('app.string.font_family_medium')) 303 .backgroundColor($r('app.color.color_palette_aux1')) 304 305 Image($r('app.media.app_icon')) 306 .border({ 307 color: $r('app.color.color_palette_aux1'), 308 radius: $r('app.float.corner_radius_button'), width: 2 309 }) 310 .margin({ 311 top: $r('app.float.elements_margin_horizontal_m'), 312 bottom: $r('app.float.elements_margin_horizontal_l') 313 }) 314 .height(200) 315 .width(300) 316 317 // For a string in **string.json** whose name is "message_notification" and value is "Hello, %1$s!,You have %2$d new messages.", 318 // replace the placeholders $s and $d with 'LiHua' and 2, respectively. The sample code is as follows: 319 Text($r('app.string.message_notification', 'LiHua', 2)) 320 ``` 321 322- After obtaining a **ResourceManager** object through the application context, call APIs of [resource management](../reference/apis-localization-kit/js-apis-resource-manager.md) to access different resources. Example:<br>Call **getContext().resourceManager.getStringByNameSync('test')** to obtain string resources.<br>Call **getContext().resourceManager.getRawFd('rawfilepath')** to obtain the descriptor information of the HAP where the raw file is located, and then use **{fd, offset, length}** to access the raw file. 323 324### Cross-HAP/HSP Resources 325 326<!--Del--> 327**Cross-Bundle Access (for System Applications Only)** 328 329- Create the context of the corresponding HAP or HSP file through the [createBundleContext(context, bundleName)](../reference/apis-ability-kit/js-apis-app-ability-application-sys.md#applicationcreatebundlecontext12) API. After obtaining the **resourceManager** object, call different APIs of [resource management](../reference/apis-localization-kit/js-apis-resource-manager.md) to access various resources by resource ID or resource name. 330<!--DelEnd--> 331 332**Inter-Bundle, Cross-Module Access** 333 334- Create the context of the corresponding module through the [createModuleContext(context, moduleName)](../reference/apis-ability-kit/js-apis-app-ability-application.md#applicationcreatemodulecontext12) API. After obtaining the **resourceManager** object, call different APIs of [resource management](../reference/apis-localization-kit/js-apis-resource-manager.md) to access various resources by resource ID or resource name. 335 336- Access resources through **$r()** or **$rawfile()**. Specifically, perform either of the following: 337 338 1. Add dependencies to the **oh-package.json5** file in the **entry** directory. For example, **"dependencies": {"library": "file:../library"}**. 339 340  341 342 2. Obtain resources by using the literal **[hsp].*type*.*name***, where **hsp** indicates the HSP module name, **type** indicates the resource type, and **name** indicates the resource name. The following is an example: 343 344 ```ts 345 Text($r('[hsp].string.test_string')) 346 .fontSize($r('[hsp].float.font_size')) 347 .fontColor($r('[hsp].color.font_color')) 348 Image($rawfile('[hsp].icon.png')) 349 ``` 350 3. Use variables. The following is an example: 351 352 ```ts 353 @Entry 354 @Component 355 struct Index { 356 text: string = '[hsp].string.test_string'; 357 fontSize: string = '[hsp].float.font_size'; 358 fontColor: string = '[hsp].color.font_color'; 359 image: string = '[hsp].media.string'; 360 rawfile: string = '[hsp].icon.png'; 361 362 build() { 363 Row() { 364 Text($r(this.text)) 365 .fontSize($r(this.fontSize)) 366 .fontColor($r(this.fontColor)) 367 368 Image($r(this.image)) 369 370 Image($rawfile(this.rawfile)) 371 } 372 } 373 } 374 ``` 375 > **NOTE** 376 > 377 > The HSP module name must be placed in the brackets ([]). If the **rawfile** directory contains multiple levels of folders, the path must start from the first level, for example, **$rawfile('[hsp].oneFile/twoFile/icon.png')**. When **$r** or **$rawfile** is used for cross-HSP resource access, resource verification is not available at compile time, and you need to manually check that the target resources exist in the corresponding location. 378 379 380### System Resources 381 382You can obtain system icon resources from [HarmonyOS Symbol](https://developer.huawei.com/consumer/en/design/harmonyos-symbol/) and system color, spacing, and rounded corner resources from the [system resource layer design table](https://gitcode.com/openharmony/docs/blob/master/en/design/ux-design/design-resources.md). In addition, you can obtain system color resources from [full table of basic and semantic tokens](https://developer.huawei.com/consumer/en/doc/design-guides/color-0000001776857164#section17672143841113). The resources in this table do not overlap with those in the system resource layer design table. Both are recommended system color resources. 383You can further set the icon colors through [SymbolGlyph](../reference/apis-arkui/arkui-ts/ts-basic-components-symbolGlyph.md). 384 385To access system resources, use the **$r('sys.type.name')** format, where **sys** indicates the system resource, **type** indicates the resource type, such as color, float, string, media, or symbol, and **name** indicates the resource name. 386 387> **NOTE** 388> - Other system resources that are not listed in the mentioned tables are system component and underlying capability parameters. You are advised to use the system resources provided in the preceding tables. 389> 390> - For preset applications, you are advised to use system resources. For third-party applications, you can choose to use system resources or custom application resources as required. 391> 392> - The use of system resources is only supported in the declarative development paradigm. 393> 394> - Currently, the default system font used on the UI is HarmonyOS Sans, and the supported character range follows the standards in [Information technology—Chinese coded character set GB18030-2022](https://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=A1931A578FE14957104988029B0833D3). If the characters cannot be displayed in HarmonyOS Sans, the system uses another font with the highest priority to display the characters. For details about the priority of the system fonts, see the `system/etc/fontconfig.json` configuration file on the device. 395 396```ts 397Text('Hello') 398 .fontColor($r('sys.color.ohos_id_color_emphasize')) 399 .fontSize($r('sys.float.ohos_id_text_size_headline1')) 400 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')) 401 .backgroundColor($r('sys.color.ohos_id_color_palette_aux1')) 402 403Image($r('sys.media.ohos_app_icon')) 404 .border({ 405 color: $r('sys.color.ohos_id_color_palette_aux1'), 406 radius: $r('sys.float.ohos_id_corner_radius_button'), width: 2 407 }) 408 .margin({ 409 top: $r('sys.float.ohos_id_elements_margin_horizontal_m'), 410 bottom: $r('sys.float.ohos_id_elements_margin_horizontal_l') 411 }) 412 .height(200) 413 .width(300) 414``` 415 416## Matching Resources 417 418When your application needs to use a resource, the system preferentially searches the qualifiers subdirectories that match the current device state. The system searches the **base** subdirectory for the target resource only when the **resources** directory does not contain any qualifiers subdirectories that match the current device state or the target resource is not found in the qualifiers subdirectories. **rawfile** and **resfile** are raw file directories, which will not match resources based on the device status. 419 420> **NOTE** 421> 422> - The resource files in the **AppScope** directory are merged into the **resources** directory. If files with the same name exist in these two directories, the ones in the **AppScope** directory are retained after build and packaging. 423 424### Rules for Matching Qualifiers Subdirectories and Device Resources 425 426- Qualifiers are matched with the device resources in the following priorities: MCC&MNC > locale (options: language, language_script, language_country/region, and language_script_country/region) > screen orientation > device type > color mode > screen density 427 428- If the qualifiers subdirectories contain the MCC, MNC, language, script, screen orientation, device type, and color mode qualifiers, their values must be consistent with the current device status so that the subdirectories can be used for matching the device resources. For example, the qualifiers directory **zh_CN-car-ldpi** cannot be used for matching the resource files labeled **en_US**. 429 430- If there are multiple qualifiers subdirectories for the screen density, the closest qualifiers are matched upwards; otherwise, downwards. For example, if qualifiers directories **xldpi** and **xxldpi** exist and the device screen density is **xxldpi**, the qualifier directory **xxldpi** will be matched. 431 432For details about the rules for loading resources on the application UI, see [Overview of Internationalization and Localization](../internationalization/i18n-l10n.md). 433 434### Obtaining Resources for a Specific Configuration 435 436**Basic Concepts** 437 438You can add qualifiers to the project-level **resources** directory to meet different system settings such as multi-language support and dark and light color modes. However, some directory resources fail to be obtained due to matching rules of the qualifiers directories. 439 440If this is the case, you can use the following APIs to obtain resources for a specific configuration. 441 442**Available APIs** 443 444| API | Description | 445| ------------------------------------------------------------ | ------------------------------------------------------------ | 446| [getOverrideResourceManager](../reference/apis-localization-kit/js-apis-resource-manager.md#getoverrideresourcemanager12)(configuration?: [Configuration](../reference/apis-localization-kit/js-apis-resource-manager.md#configuration)) : [ResourceManager](../reference/apis-localization-kit/js-apis-resource-manager.md#resourcemanager) | Obtains a **ResourceManager** object for loading resources for a specific configuration. This API returns the result synchronously.| 447| [getOverrideConfiguration](../reference/apis-localization-kit/js-apis-resource-manager.md#getoverrideconfiguration12)() : [Configuration](../reference/apis-localization-kit/js-apis-resource-manager.md#configuration) | Obtains the specified configuration. This API returns the result synchronously. | 448| [updateOverrideConfiguration](../reference/apis-localization-kit/js-apis-resource-manager.md#updateoverrideconfiguration12)(configuration: [Configuration](../reference/apis-localization-kit/js-apis-resource-manager.md#configuration)) : void | Updates the specified configuration. | 449 450**Example** 451 452The following example demonstrates how to obtain the specified resources for languages other than the one in use. Assume that the following resource files with the same name are defined in the **resources** directories for Chinese, English, and German: 453 454- entry/src/main/resources/zh_CN/element/string.json 455 456```json 457{ 458 "string": [ 459 { 460 "name": "greetings", 461 "value": "你好,世界" 462 } 463 ] 464} 465``` 466 467- entry/src/main/resources/en_US/element/string.json 468 469```json 470{ 471 "string": [ 472 { 473 "name": "greetings", 474 "value": "Hello, world" 475 } 476 ] 477} 478``` 479 480- entry/src/main/resources/de_DE/element/string.json 481 482```json 483{ 484 "string": [ 485 { 486 "name": "greetings", 487 "value": "Hallo, Welt" 488 } 489 ] 490} 491``` 492 493In **Index.ets**, add code to obtain the resources of the three languages and display them in the text box. The current system language is Chinese. The following is sample code in **entry/src/main/ets/pages/Index.ets**: 494 495```ts 496import { common } from '@kit.AbilityKit'; 497 498@Entry 499@Component 500struct Index { 501 @State englishString: string = ""; 502 @State germanString: string = ""; 503 504 getString(): string { 505 let resMgr = this.getUIContext().getHostContext()?.resourceManager; 506 if (!resMgr) { 507 return ""; 508 } 509 let currentLanguageString: string = ""; 510 try { 511 let resId = $r('app.string.greetings').id; 512 513 // Obtain resources that match the current system configuration, including the color mode and resolution, for the current system language. 514 currentLanguageString = resMgr.getStringSync(resId); 515 516 // Obtain resources that match the current system configuration, including the color mode and resolution, for the English language. 517 let overrideConfig = resMgr.getOverrideConfiguration(); 518 overrideConfig.locale = "en_US"; // Set the language to English and locale to en_US. 519 let overrideResMgr = resMgr.getOverrideResourceManager(overrideConfig); 520 this.englishString = overrideResMgr.getStringSync(resId); 521 522 // Obtain resources that match the current system configuration, including the color mode and resolution, for the German language. 523 overrideConfig.locale = "de_DE"; // Set the language to German and locale to de_DE. 524 overrideResMgr.updateOverrideConfiguration(overrideConfig); // Equivalent to resMgr.updateOverrideConfiguration(overrideConfig). 525 this.germanString = overrideResMgr.getStringSync(resId); 526 } catch (err) { 527 const code = (err as BusinessError).code; 528 const message = (err as BusinessError).message; 529 console.error(`get override resource failed, error code: ${code}, error msg: ${message}`); 530 } 531 return currentLanguageString; 532 } 533 534 build() { 535 Row() { 536 Column() { 537 Text(this.getString()) 538 .fontSize(50) 539 .fontWeight(FontWeight.Bold) 540 Text(this.englishString) 541 .fontSize(50) 542 .fontWeight(FontWeight.Bold) 543 Text(this.germanString) 544 .fontSize(50) 545 .fontWeight(FontWeight.Bold) 546 } 547 .width('100%') 548 } 549 .height('100%') 550 } 551} 552``` 553 554## Overlay Mechanism 555 556Overlay is a resource replacement mechanism. With overlay resource packages, you can enable your application UI to adapt to different styles of various brands and products, without having to repack your application HAPs. The overlay mechanism works in dynamic and static modes. Overlay resource packages contain only resource files, resource index files, and configuration files. 557 558The overlay feature is enabled by default. For details about how to enable and disable this feature, see [@ohos.bundle.overlay (overlay)](../reference/apis-ability-kit/js-apis-overlay.md). 559 560### Using overlay in dynamic mode 561 5621. Place the overlay resource package in the target application installation path and install the package using **hdc install**. For example, for the com.example.overlay application, place the overlay resource package in **data/app/el1/bundle/public/com.example.overlay/**. 563 5642. The application uses [addResource(path)](../reference/apis-localization-kit/js-apis-resource-manager.md#addresource10) to load overlay resources and uses [removeResource(path)](../reference/apis-localization-kit/js-apis-resource-manager.md#removeresource10) to remove overlay resources. The path to an overlay resource consists of the application's sandbox root directory (obtained through **getContext().bundleCodeDir**) and the overlay resource bundle name. For example, **let path = getContext().bundleCodeDir + "overlay *resource bundle name*"**, such as **/data/storage/el1/bundle/overlayResourcePackageName**. 565 566### Using overlay in static mode 567 568The **app.json5** file in the inter-application overlay resource package supports the following fields: 569```json 570{ 571 "app":{ 572 "bundleName": "com.example.myapplication.overlay", 573 "vendor" : "example", 574 "versionCode": "1000000", 575 "versionName": "1.0.0.1", 576 "icon": "$media:app_icon", 577 "label": "$string:app_name" 578 } 579} 580``` 581The **module.json5** file in the inter-application overlay resource package supports the following fields: 582```json 583{ 584 "module":{ 585 "name": "entry_overlay_module_name", 586 "type": "shared", 587 "description": "$string:entry_overlay_desc", 588 "deviceTypes": [ 589 "default", 590 "tablet" 591 ], 592 "deliverywithInstall": true, 593 "targetModuleName": "entry_module_name", 594 "targetPriority": 1 595 } 596} 597``` 598<!--Del--> 599The **app.json5** file in the cross-application overlay resource package supports the following fields, which are available for system applications only: 600```json 601{ 602 "app":{ 603 "bundleName": "com.example.myapplication.overlay", 604 "vendor" : "example", 605 "versionCode": "1000000", 606 "versionName": "1.0.0.1", 607 "icon": "$media:app_icon", 608 "label": "$string:app_name", 609 "targetBundleName": "com.example.myapplication", 610 "targetPriority": 1 611 } 612} 613``` 614The **module.json5** file in the cross-application overlay resource package supports the following fields, which are available for system applications only: 615```json 616{ 617 "module":{ 618 "name": "entry_overlay_module_name", 619 "type": "shared", 620 "description": "$string:entry_overlay_desc", 621 "deviceTypes": [ 622 "default", 623 "tablet" 624 ], 625 "deliverywithInstall": true, 626 "targetModuleName": "entry_module_name", 627 "targetPriority": 1 628 } 629} 630``` 631<!--DelEnd--> 632> **NOTE** 633><!--Del--> 634> - **targetBundleName**: name of the target application to apply the overlay feature. The value is a string. 635><!--DelEnd--> 636> - **targetModuleName**: name of the target module to apply the overlay feature. The value is a string. 637> 638> - **targetPriority**: overlay priority. The value is an integer. 639> 640> - Other fields such as **Ability**, **ExtensionAbility**, and **Permission** are not supported. 641> 642> - The overlay feature does not support JSON images. 643 644If the **module.json5** file of a module contains the **targetModuleName** and **targetPriority fields** during project creation on DevEco Studio, the module is identified as a module with the overlay feature in the installation phase. Modules with the overlay feature generally provide an overlay resource file for other modules on the device, so that the module specified by **targetModuleName** can display different colors, labels, themes, and the like by using the overlay resource file in a running phase. 645 646 <!--no_check-->