How to access Plurals in Java code for Harmony OS?

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

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();

        }

    }

}

Leave a Comment