applibgroup / HarmonyOS-Knowledgebase

This repository contains code samples of HarmonyOS Training
Apache License 2.0
15 stars 6 forks source link

How to access Plurals in Java code for Harmony OS? #53

Closed shreyshrivastava4799 closed 3 years ago

shreyshrivastava4799 commented 3 years ago

Describe the query

I have defined some strings which I can access using Resource Table. How can I define and use Plurals similarly?

Create the query with harmonyos tag in stackoverflow and share the link here:

**** https://stackoverflow.com/questions/68591851/how-to-access-plurals-in-java-code-for-harmony-os

Additional information

Developer Platform: Windows DevEco Studio version: 2.1.0.303 SDK API version: 5 SDK version: 2.1.1.21 Device: Not Required Device OS version: Not Required

samuel-abhishek commented 3 years ago

Answered by Shirley in Stackoverflow : Are you asking how to use the plural.json resource file? If so, you can refer to the following code:

1.plural.json sample code

{

    "plural":[

        {

            "name":"eat_apple",

            "value":[

                {

                    "quantity":"one",

                    "value":"%d apple"

                },

                {

                    "quantity":"other",

                    "value":"%d apples"

                }

            ]

        }

    ]

}

2.The following is an example of using the MainAbilitySlice.java file:

public class MainAbilitySlice extends AbilitySlice {

    private static final HiLogLabel LOG_LABEL = new HiLogLabel(3, 0xD001100, "MainAbilitySlice");

    @Override

    public void onStart(Intent intent) {

        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_ability_main);

        String pluralString = null;

        try {

            pluralString = getResourceManager().getElement(ResourceTable.Plural_eat_apple).getPluralString(1, 1);

            HiLog.info(LOG_LABEL, pluralString);

            pluralString = getResourceManager().getElement(ResourceTable.Plural_eat_apple).getPluralString(2, 10);

            HiLog.info(LOG_LABEL, pluralString);

        } catch (IOException e) {

            e.printStackTrace();

        } catch (NotExistException e) {

            e.printStackTrace();

        } catch (WrongTypeException e) {

            e.printStackTrace();

        }

    }

}