Home > OS >  Getting info from .properties file Java
Getting info from .properties file Java

Time:03-03

I have to write a code which translates numbers into words and it's supposed to be universal for different languages. For this purpose I have a .properties file which looks something like this.

40=forty

50=fifty

80=eighty

teen=teen

tens-suffix=ty

tens-after-delimiter=-

This is my code so far:

 public String numberInWords(Integer number) {

    Properties properties = new Properties();
    FileInputStream is = null;

    try {
        is = new FileInputStream(filePath);

        InputStreamReader reader = new InputStreamReader(
                is, StandardCharsets.ISO_8859_1);

        properties.load(reader);
    } catch (Exception e) {
        // handle exceptions
    } finally {
        close(is);
    }

I know that I can get number values like this:

properties.getProperty(String.valueOf(number));

But how am I supposed to add the value "teen" to the result string? I tried something along the lines like the last line of code but my IDE says it cannot resolve the symbol.

Thanks

Edit:

This was an example property file. I am well aware that adding "teen" won't work with other languages. In my property files I already have exceptions like 11 - eleven, 12 - twelve and so on. For example in my estonian property file I have a line:

teen=teist,

this will work on all estonian numbers. I need to find a way to add the teen value to a string. If an input is estonian, it will use the estonian property file and so on. This is determined in my other method.

I am trying to find a method that allows me to get the value teen="something" from a property file so I can add this to the string. Every property file is separate and has exceptions in them.

This line won't work, because it cannot resolve symbol teen:

properties.getProperty(String.valueOf(teen));

I am looking for a solution for this exact probleem

CodePudding user response:

You need to work with bundle resources. That allows you to read the same property from different property files based on your current locale. Read this article: Backing a ResourceBundle with Properties Files

CodePudding user response:

This won't work. I don't think you can create a universal translator like that because all languages treat their numbers differently. While the english language will append teen to numbers between 13 and 19, other languages don't do it like that. Also simply appending teen won't even work for English.

Append teen to three will be threeteen and not thirteen.

  •  Tags:  
  • java
  • Related