2

I'm writing small app and I need to write duration of sport event in i18n. Im using PrettyTime library for date, but when I attempt to use DateUtils or PrettyTime, I have issues..

For example I want to say that duration is 2 minutes. I need some way to pass it to library which supports i18n and accept milliseconds and return Chars.

In android we have:

com.android.internal.R.plurals.duration_minutes

But I can't access to it from my App. Is there any way to make it using correct way and not writing own plurals for all languages?

Thank you

kurd
  • 467
  • 1
  • 10
  • 18
  • You're going to need to create your own string and hire translators. There's no built in translations for that because there's no way to know what you really mean and not have subtle translation issues. For example I had an issue where I just used a numeric date and it wasn't understnadable in Korean- they wanted NNday where day was in Korean. Depending on where you're using it the translation can switch GREATLY. You can't provide good translations without context, so Android doesn't try. – Gabe Sechan Mar 17 '15 at 22:42

1 Answers1

1

I am not sure which issues you are talking about in context of Android-DateUtils and PrettyTime-library. But I know for sure that Android-DateUtils does not perfectly manage the plural rules of various languages (especially not slavish languages or arabic because it only knows singular and one plural form which is too simple). See for example this Android-issue. About the PrettyTime-library, the same objection is valid if you consider Arabic - see the source.

My recommendation:

Try out my library Time4A (a new AAR-library). Then you can use this code to process a millisecond-input and to produce a localized minute-string:

// input
long millis = 1770123;

// create a duration
Duration<ClockUnit> duration = Duration.of(millis, ClockUnit.MILLIS); 

// normalization to (rounded) minutes
duration = duration.with(ClockUnit.MINUTES.rounded());

String s = PrettyTime.of(Locale.ENGLISH).print(duration, TextWidth.WIDE);
System.out.println(s); // 30 minutes

Example for Korean (answer to comment of @Gabe Sechan):

String s = PrettyTime.of(new Locale("ko")).print(duration, TextWidth.WIDE);
System.out.println(s); // 30분 (korean translation of "30 minutes")

Example for Arabic (right to left):

String s = PrettyTime.of(new Locale("ar")).print(duration, TextWidth.WIDE);
System.out.println(s); // ٣٠ دقيقة

This solution currently supports ~90 languages (more than in PrettyTime-library) and three text widths (full, abbreviated or narrow). Accurate pluralization handling is automatically included. Time4A uses its own language resources based on CLDR-data (independent from Android). But you are free to override those resources by defining your own assets (in UTF-8).

About normalization: I just showed the variant which you have described in your question. However, there are many more ways how to normalize durations in Time4A(J). This page will give you more ideas how to use that feature.

If you still miss some languages then just tell me, and I will support it in the next versions of Time4A. Currently supported languages can be found in the tutorial.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126