Henning Hall
Henning Hall

Reputation: 1457

Print time-related strings like "today" and "yesterday" translated depending on Locale?

I am looking for a way to print time-related strings like "Today" and "Yesterday" translated to the user's Locale (i.e language) in an Android app.

I have tried to use DateUtils.getRelativeDateTimeString but it does not consider the locale, or at least it is not possible to change it during runtime. I would need something similar to this existing constructor of SimpleDateFormat:

public SimpleDateFormat(String pattern, Locale locale)

Of course, it would be possible to add translations of each word in every language but isn't there a better way assuming the words actually exists in the OS.

Is there a way to achieve this?

Edit: I am trying to do something similar to the date picker on iOS:

Upvotes: 3

Views: 492

Answers (2)

Henning Hall
Henning Hall

Reputation: 1457

With @Meno Hochschild' comment I was able to solve it by:

  1. Adding the Time4A library.
  2. Made a local copy of UnitPatterns.java and made it public.
  3. Used it like this:

UnitPatterns.of(locale).getTodayWord(); // returns "today" UnitPatterns.of(locale).getYesterdayWord(); // returns "yesterday"

Upvotes: 2

DwB
DwB

Reputation: 38320

This appears to be a translation problem, not a date-format problem.

Your app will have messages that it wants to display to the user. These messages will need to be translated into your target languages. Once you know the message you want to display, retrieve then display the localized (i.e. translated) message using the locale (Android supports that).

It is likely that you do you actually want to display the word "tomorrow" or "yesterday". Instead, you probably want to display something like "blah blah tomorrow" or "sorry blah blah yesterday". Translate the entire message, not just the words individually.

Additional Info I think you do need to translate "Today" into every target language. And, you need to translate each of the date related strings into every target language (by which I mean "Tue May 1" needs to be translated into every language). There are "tricks" you can use. Choose the "base" part of the string and use placeholders for the other parts. Placeholders, because different languages may have different orders for "Day-of-week Month Day"

Upvotes: 0

Related Questions