0

This is in an Android app, but the question might apply to Java/OOP in general as well.

In my app I have a class Job, which represents a job for the user to complete. One of its properties is status, which can only have three values: 'to be done', 'pending', or 'completed'.

I want to allow for translation of the app into other languages, so I have been storing these values using Android's string-array resource type in XML:

<string-array name="jobs_statuses">
    <item>To be done</item>
    <item>Pending</item>
    <item>Completed</item>
</string-array>

I want to be able to make comparisons using the status property in my code, for instance:

if (myJob.getStatus() == Job.STATUS_COMPLETED) // Do something

I thought about storing the property in the class as an int or other numerical type, and declaring three constants in the class like so:

public static final int STATUS_TO_BE_DONE = 1;
public static final int STATUS_PENDING = 2;

This would allow for easily determining the status of a given job in my code, however, how do I then get a string value back for use in my UI? I've considered having a separate getStatusString method which might be used like so:

myJob.getStatus(); // returns 1
myJob.getStatusString(); // returns localised string for 'To be done' status

So, in short, I want to be able to:

  • Make comparisons using this property
  • Get a string value for this property, which is localised based on the user's device language (localised strings stored in XML)

Am I missing an obvious solution?

Thanks in advance.

Ellis
  • 3,048
  • 2
  • 17
  • 28
  • 2
    I'd consider an enum (the ultimate arbiter) and associated labels (the I18N). I'm not sure what the best way to handle that in Android is, I'd have to double-check. – Dave Newton Mar 12 '14 at 16:32
  • Duplicate question, see http://stackoverflow.com/questions/9742050/is-there-an-enum-string-resource-lookup-pattern-for-android – Pierre Rust Mar 12 '14 at 16:52
  • It's true that seeing that question would have lead me to a solution, but I wasn't aware of the enum data type so it was unlikely I would have found it. Also, the question being asked there is not the same as mine (they already had much more code than I did!) – Ellis Mar 12 '14 at 20:13

2 Answers2

1

As Dave Newton already suggested, I'd do something like this:

enum Status {

    TO_BE_DONE(R.string.to_be_done),
    PENDING(R.string.pending),
    COMPLETED(R.string.completed);

    private final int mStringResource;

    private Status(final int stringResource) {
        mStringResource = stringResource;
    }

    public String asString(final Context context) {
        return context.getString(mStringResource);
    }

}

which allows you to do comparison using ==, as well as getting a localised String through asString() method.

Tadej
  • 2,901
  • 1
  • 17
  • 23
  • Thanks, this is just what I needed! I modified it a little to work with the `string-array` resource, because it's useful in other areas of the app for me to use that rather than a bunch of `string` types. – Ellis Mar 12 '14 at 20:10
0

This may not be the exact answer to the title of your question but if your looking for and easy way to translate your app for different languages Google does indeed offer a service for this. http://developer.android.com/distribute/googleplay/publish/localizing.html

Patty P
  • 351
  • 2
  • 12