Classification of resource files
Resources directory
Application resource files (strings, images, audio, etc.) are stored in the resources directory, which is easy for developers to use and maintain. The resources directory contains two types of directories: base and qualifier directories, and rawfile directories.
Resources | - base / / the default existing directory | | element - | | | - string. The json | | - media | | | - icon. The PNG | en_GB - vertical - car - mdpi / / qualifier example, require developers to create | | element - | | | - string. The json | | - media | | | - icon. The PNG | - rawfile / / the default directoryCopy the code
A list of determiners
Qualifier directory can be characterized by one or more application scenarios or equipment characteristics of the qualifier, including mobile country code and mobile network code, spoken and written languages, country or region, namely screen, equipment type, color, pattern and density of the screen dimension, qualifier between by scribing underscore (_) or (-) connection. When creating a qualifier directory, developers need to understand the naming requirements of the qualifier directory and the rules for matching the qualifier directory and device state.
Naming requirements for a list of qualifiers
- The combination order of qualifiers is: Mobile country code _ Mobile network code _ Language _ Text _ Country/region _ Portrait/Device type _ Dark mode _ Screen density. Developers can select one or more of these qualifiers to form the directory name, depending on the application usage scenario and device characteristics.
- The way qualifiers are connected: languages, characters, countries or regions are underlined (), and underline between the mobile country code and the mobile network code (), all other qualifiers are connected by hyphens (-). For example, zh_Hant_CN and zh_cn-car-ldpi.
- Value range of qualifiers: The value of each type of qualifier must match the conditions in Table 2, otherwise the resource file in the directory will not be matched.
The matching rule between the qualifier catalog and the device state
- When matching resource files for devices, the priority of qualifier directory matching from high to low is as follows: Mobile country code and mobile network code > Region (language _ Text _ Country or region) > Horizontal and Vertical screen > Device type > Color mode > Screen density.
- If the qualifier directory contains mobile country code, mobile network code, language, text, horizontal and vertical screen, device type, and color mode qualifiers, the directory can participate in device resource matching only when the value of the corresponding qualifier is consistent with the current device status. For example, the zh_CN-car-ldpi qualifier directory cannot be used to match the en_US device.
Resource Group directory
Under the base directory and qualifier directory, you can create resource group directories (including Element, media, Animation, Layout, Graphic, and Profile) to store a specific type of resource file element: To represent element resources, each of the following categories of data is represented by the corresponding JSON file. Media: media resources, including non-text files such as pictures, audio files, and videos. Animation: Animation resources in the XML format. Layout: indicates the layout of resources in XML format. Graphic: Indicates the resources that can be drawn. The file format is XML. Profile: indicates other types of files and saves them as original files.
Creating a resource File
Under the resources directory, you can follow the instructions for the qualifier directory and the resource group directory to create subdirectories and files within the directory. DevEco Studio also provides an interface for creating resource directories and resource files.
- Create resource directories and resource files
Right-click the Resources directory and choose New > Harmony Resource File. You can create a directory and File at the same time. By default, files are created in the resource group corresponding to the base directory. If a qualifier is selected, the qualifier + resource group directory is automatically generated according to the naming convention and the file is created in the directory. The directory name is automatically generated. The format is determiner. Resource group: For example, when you create a portrait in the horizontal and portrait category, the resource group is the directory for drawing resources, and the directory name is vertical.graphic.
- Creating a resource Directory
Right-click the Resources Directory and choose New > Harmony Resource Directory from the shortcut menu. Select a resource group type, set a qualifier, and automatically generate a directory name after the resource group is created. The format of the directory name is determiner. Resource group: For example, when you create a portrait in the horizontal and portrait category, the resource group is the directory for drawing resources, and the directory name is vertical.graphic.
- Creating a resource File
Right-click a Resource directory and choose New > XXX Resource File to create a Resource File for the corresponding Resource group directory. For example, you can create an Element Resource File in the Element directory.
Use of resource files
Reference methods to resource files
Resource files in the base directory and qualifier directory: Referenced by specifying resource type (type) and resource name (name).
- Java files refer to the format of resource files:ResourceTable.type_name. In particular, if a reference is to a system resource, then:ohos.global.systemres.ResourceTable.type_name.
- Example 1: In a Java file, reference the resource whose type is String and name is app_name in the string.json file.
ohos.global.resource.ResourceManager resManager = this.getResourceManager(); String result = resManager.getElement(ResourceTable.String_app_name).getString(); Copy the code
- Example 2: Reference the resource whose type is color and name is red in the color.json file in the Java file.
ohos.global.resource.ResourceManager resManager = this.getResourceManager(); int color = resManager.getElement(ResourceTable.Color_red).getColor(); Copy the code
- The format of the resource file referenced by the XML file is type:name. In particular, if a reference is to a system resource, : type:name is used. In particular, if a reference is to a system resource, : type:name is used. In particular, if a reference is to a system resource, use: ohos:type:name. The following is an example of referencing the resource whose type is String and name is app_name in the string.json file in the XML file:
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:orientation="vertical">
<Text ohos:text="$string:app_name"/>
</DirectionalLayout>
Copy the code
Resource files in the rawfile directory: Referenced by specifying the file path and file name. The following is an example of a Java file that references a resource file named example.js whose path is resources/rawfile/ :
ohos.global.resource.ResourceManager resManager = this.getResourceManager();
ohos.global.resource.RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/example.js");
Copy the code
System resource file
Ic_app: represents the default icon for a HarmonyOS application. Request_location_reminder_title: Indicates the prompt title for request to use device location functionality. Request_location_reminder_content: indicates a request to use device location, i.e. please enable location information in the dropdown shortcut bar.
Definition of the color pattern
An application can define the “colorMode” field under the module field of config.json. The “colorMode” field is used to define the colorMode of the application. Example:
"colorMode": "light"
Copy the code
When the applied color mode value is “dark”, no matter what the current color mode is, the application will always select resources according to the dark mode; Similarly, when the color mode value of the application is “light”, no matter what the current color mode of the system is, the application will always select resources according to the light color mode. When the applied color mode value is Auto, the application selects resources according to the system color mode value. An application can obtain the current color mode of the application in code by:
int colorMode = Configuration.colorMode;
Copy the code
Add comments or special identifiers to the Element resource file
The resources of different types of elements in the Element directory are represented by JSON files. The name and value of resources are mandatory fields for each resource.
- If you need to comment for a resource to facilitate the understanding and use of the resource, you can use the comment field to add comments.
- If some text in the value field does not need to be processed by the translator or displayed on the application interface, a special structure can be used to identify the content that does not need to be translated.
Add comments using the COMMENT field
The COMMENT field allows you to comment the resources of a JSON file. The following is an example:
{
"string":[
{
"name":"message_arrive"."value":"We will arrive at %s"."comment":"Transfer Arrival Time. %s is time,like 5:00 am"}}]Copy the code
Special structures are used to identify content that does not need to be translated
In the string, strarray, and plural resources, you can use special identifiers to handle content that does not need to be translated. For example, a string resource with a Value of “We will arrive at %s” in which the variable “%s” is expected to remain unchanged during translation. There are two ways to handle the problem:
- Method 1: Add {} to the value field. The following is an example:
{
"string":[
{
"name":"message_arrive"."value": ["We will arrive at", {"id":"time"."example":"5:00 am"."value":"%s"}]}]}Copy the code
- Method 2: Add the
tag pair. The following is an example:
{
"string":[
{
"name":"message_arrive"."value":"We will arrive at <xliff:g id='time' example='5:00 am'>%s</xliff:g>"}}]Copy the code
Boolean. Json example
{
"boolean":[
{
"name":"boolean_1"."value":true
},
{
"name":"boolean_ref"."value":"$boolean:boolean_1"}}]Copy the code
Color. The json example
{
"color":[
{
"name":"red"."value":"#ff0000"
},
{
"name":"red_ref"."value":"$color:red"}}]Copy the code
Float. Json example
{
"float":[
{
"name":"float_1"."value":"30.6"
},
{
"name":"float_ref"."value":"$float:float_1"
},
{
"name":"float_px"."value":"100px"}}]Copy the code
Intarray. Json example
{
"intarray":[
{
"name":"intarray_1"."value": [100.200."$integer:integer_1"]]}}Copy the code
Integer. The json example
{
"integer":[
{
"name":"integer_1"."value":100
},
{
"name":"integer_ref"."value":"$integer:integer_1"}}]Copy the code
The pattern. The json example
{
"pattern":[
{
"name":"base"."value":[
{
"name":"width"."value":"100vp"
},
{
"name":"height"."value":"100vp"
},
{
"name":"size"."value":"25px"}]}, {"name":"child"."parent":"base"."value":[
{
"name":"noTitile"."value":"Yes"}]}]}Copy the code
Plural. Json example
{
"plural":[
{
"name":"eat_apple"."value":[
{
"quantity":"one"."value":"%d apple"
},
{
"quantity":"other"."value":"%d apples"}]}]}Copy the code
Strarray. Json example
{
"strarray":[
{
"name":"size"."value":[
{
"value":"small"
},
{
"value":"$string:hello"
},
{
"value":"large"
},
{
"value":"extra large"}]}]}Copy the code
String. The json example
{
"string":[
{
"name":"hello"."value":"hello base"
},
{
"name":"app_name"."value":"my application"
},
{
"name":"app_name_ref"."value":"$string:app_name"
},
{
"name":"app_sys_ref"."value":"$ohos:string:request_location_reminder_title"}}]Copy the code
Support for internationalization capability
Time and Date internationalization
Different areas have different time and date display habits. For example, the Short time format in the English (US) area is “9:31am”; In simplified Chinese (China), the short time format is “9:31 am”. The Finnish (Finnish) region short time format is “9.31”. This provides developers with the ability to obtain the date and time specifications for different areas. The display of time and date strings and time controls on the interface should follow the local rules. To display the time or date, you are advised to obtain the time and date specifications of the current region and format the displayed string according to the obtained specifications before using it.
Example 1:
Locale locale = new Locale("de"."CH");
String skeleton = "MMMMd";
String bestPattern = DateFormatUtil.getBestPattern(skeleton, locale); // Return "d. MMMM"
Copy the code
Example 2:
String languageTag = "zh";
String out = DateFormatUtil.format("EEEEdMMMMy", languageTag, "Asia/Shanghai".0.3600 * 1000); // Return "Thursday, January 1, 1970"
Copy the code
Internationalization of telephone numbers
Phone numbers in different areas have different formatting effects. When displaying local phone numbers, follow the formatting principles of local phone numbers. This provides developers with the ability to format phone numbers in different regions so that the phone numbers can be correctly formatted when displayed. And provides the ability to get the phone number home, developers can use the relevant interface to get the phone number home information.
Example 1:
InputFormatter formatter = InputFormatter.getInstance("CN");
formatter.inputNumberAndRememberPosition('1'); // Return value "1"
formatter.inputNumber('5'); // Return value "15"
formatter.inputNumber('6'); // Return value "156"
formatter.inputNumberAndRememberPosition('1');// Return value "156 1"
Copy the code
Example 2:
Locale.Builder builder = new Locale.Builder();
builder.setLanguage("zh");
builder.setRegion("CN");
builder.setScript("Hant");
Locale locale = builder.build();
String displayName = PhoneNumberAttribution.getAttribute("+8615611xxxxxx"."CN", locale); // if x is any number, return value "Beijing"
Copy the code
Text recognition
Provides text recognition capability for address, time, date, and phone number. You can call the related interface to recognize the address, time, date, and phone number contained in a piece of text. Example:
Locale.getdefault ().getlanguage () is "en"
String source = "it is 123 test St";
int[] re = TextRecognitionUtils.getAddress(source);
if (re[0] = =1) {
result = source.substring(re[1], re[2] + 1);// Return "123 main St"
}
Copy the code
Format the weights and measures
Provide support for internationalization capability of weights and measures, which can support conversion between weights and measures systems and dimensions, and automatic conversion with different national weights and measures systems. This capability can be invoked when developing features that include weights and measures to meet the needs of users in multiple languages and different countries.
Example 1:
Locale zhCN = Locale.CHINA;
MeasureFormatter mes = MeasureFormatter.getInstance(zhCN);
mes.format(MeasureOptions.Unit.AREA_UK_ACRE,
10000,
MeasureOptions.Usage.AREA_LAND_AGRICULT,
MeasureOptions.FormatStyle.WIDE,
MeasureOptions.Style.AUTO_STYLE_ON));// Return "4,046.856 ha"
Copy the code
Example 2:
Locale enUS = Locale.US;
MeasureFormatter mes = MeasureFormatter.getInstance(enUS);
mes.format(MeasureOptions.Unit.VOLUME_US_CUP,
1000,
MeasureOptions.Unit.VOLUME_SI_LITER,
MeasureOptions.FormatStyle.WIDE));// Clean up
Copy the code
Sensitive taboo
Provides the ability to acquire politically sensitive areas, cities, and languages, as well as the ability to correct area names. Example:
Locale locale = Locale.getDefault();
ArrayList<String> result = LocaleHelperUtils.getBlockedRegions(context, locale);// The return value contains "EH" and "XK"(Western Sahara and Kosovo). These two areas are politically disputed and should be used with caution
Copy the code