ocpsoft / prettytime

Social Style Date and Time Formatting for Java
http://ocpsoft.org/prettytime/
Apache License 2.0
1.29k stars 253 forks source link

PrettyTime uses system default Locale instead of parameter Locale #191

Closed frangeveris closed 3 years ago

frangeveris commented 4 years ago

Hi. I'm using PrettyTime in an app that is catered to several countries. In each, they get to choose which language (think Canada where you can choose French/English, for example) they want the app in. Thing is, if the chosen language is A and the device default locale is B, PrettyTime always goes for B.

I've tried the following:

    val stringLocale = PreferencesManager.getSelectedLocale(ctx)// here we retrieve user's chosen language
    val savedLocale = Locale(stringLocale)
    Locale.setDefault(savedLocale)
    val config = Configuration(Resources.getSystem().configuration)
    config.setLocale(savedLocale)
    Resources.getSystem().updateConfiguration(config, null)
    return try {
        val stamp = Timestamp(timeStamp)
        val date = Date(stamp.time)
        val locale = stringLocale
        val pretty = PrettyTime(savedLocale)

        if (locale.equals("ru_RU", ignoreCase = true)) {
            pretty.format(date)
        } else {
            pretty.formatUnrounded(date)
        }
    } catch (e: Exception) {
        e.printStackTrace()
        CLog.e("TimeChange.getDate(timeStamp: $timeStamp) Exception: ${e.message}")
        null
    }

but it's useless, with my device in English and the app in Russian/French/you name it I always get the "X time ago" in English instead of the chosen language.

borysstach commented 4 years ago

You have to use locale with string as suffix of translation resource: https://github.com/mick1418/prettytime/tree/aa34e7df6daa97a057133a4dc190b1e2ddf76d42/core/src/main/java/org/ocpsoft/prettytime/i18n For example for Russian Locale("ru") because resource file name is Resources_ru.java

lincolnthree commented 3 years ago

Issue cleanup Thanks for filing this issue. We are cleaning old issues from the repository. If the issue is still reproducible, please re-open with a test case to reproduce the behavior.