Skip to content

Commit

Permalink
locale improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Jul 22, 2024
1 parent 6984a28 commit cca4563
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.habitrpg.android.habitica.extensions

import com.habitrpg.common.habitica.helpers.LanguageHelper
import java.util.Locale



fun Locale.getSystemDefault(): Locale {
return LanguageHelper.systemLocale
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ fun Date.toZonedDateTime(): ZonedDateTime? {
return this.toInstant().atZone(ZoneId.systemDefault())
}

/**
* Returns full display name in default Locale (Monday, Tuesday, Wednesday, etc.)
*/
fun ZonedDateTime.dayOfWeekString(): String {
return DayOfWeek.from(this).getDisplayName(TextStyle.FULL, Locale.getDefault())
}

fun formatter(): DateTimeFormatter =
DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendPattern("['T'][' ']")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.text.format.DateUtils
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.models.tasks.Task
import com.habitrpg.common.habitica.extensions.nameSentenceRes
import com.habitrpg.common.habitica.helpers.LanguageHelper
import com.habitrpg.shared.habitica.models.tasks.Frequency
import com.habitrpg.shared.habitica.models.tasks.TaskDifficulty
import com.habitrpg.shared.habitica.models.tasks.TaskType
Expand Down Expand Up @@ -131,7 +132,7 @@ class TaskDescriptionBuilder(private val context: Context) {

private fun withOrdinal(day: Int): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val formatter = MessageFormat("{0,ordinal}", Locale.getDefault())
val formatter = MessageFormat("{0,ordinal}", LanguageHelper.systemLocale)
formatter.format(arrayOf(day))
} else {
day.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import androidx.preference.DialogPreference
import com.habitrpg.common.habitica.helpers.LanguageHelper
import java.text.DateFormat
import java.util.Calendar
import java.util.Locale
Expand Down Expand Up @@ -49,7 +50,7 @@ class TimePreference(ctxt: Context, attrs: AttributeSet?) : DialogPreference(ctx
val calendar = Calendar.getInstance(Locale.getDefault())
calendar.set(Calendar.HOUR_OF_DAY, getHour(timeval))
calendar.set(Calendar.MINUTE, getMinute(timeval))
val formatter = DateFormat.getTimeInstance(DateFormat.SHORT)
val formatter = DateFormat.getTimeInstance(DateFormat.SHORT, LanguageHelper.systemLocale)
super.setSummary(formatter.format(calendar.time))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class ArmoireActivity : BaseActivity() {
lastValue = value

binding.titleView.text =
text.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
text.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
binding.equipButton.visibility = if (type == "gear") View.VISIBLE else View.GONE
when (type) {
"gear" -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.habitrpg.android.habitica.ui.views.HabiticaSnackbar
import com.habitrpg.android.habitica.ui.views.HabiticaSnackbar.SnackbarDisplayType
import com.habitrpg.android.habitica.ui.views.dialogs.HabiticaAlertDialog
import com.habitrpg.common.habitica.extensions.loadImage
import com.habitrpg.common.habitica.helpers.LanguageHelper
import com.habitrpg.common.habitica.helpers.MainNavigationController
import com.habitrpg.common.habitica.helpers.RecyclerViewState
import com.habitrpg.common.habitica.helpers.launchCatching
Expand All @@ -57,6 +58,7 @@ import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import java.text.DateFormat
import java.text.SimpleDateFormat
import javax.inject.Inject
import kotlin.math.floor
Expand Down Expand Up @@ -96,7 +98,7 @@ class FullProfileActivity : BaseActivity() {
private var attributePerSum = 0f
private var attributeDetailsHidden = true
private val attributeRows = ArrayList<TableRow>()
private val dateFormatter = SimpleDateFormat.getDateInstance()
private val dateFormatter = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, LanguageHelper.systemLocale)
private lateinit var binding: ActivityFullProfileBinding

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import com.habitrpg.android.habitica.ui.viewmodels.MainUserViewModel
import com.habitrpg.android.habitica.ui.views.CompletedAt
import com.habitrpg.android.habitica.ui.views.HabiticaIconsHelper
import com.habitrpg.android.habitica.ui.views.UserRow
import com.habitrpg.common.habitica.helpers.LanguageHelper
import com.habitrpg.common.habitica.helpers.MainNavigationController
import com.habitrpg.common.habitica.theme.HabiticaTheme
import com.habitrpg.shared.habitica.models.tasks.TaskType
Expand Down Expand Up @@ -321,7 +322,7 @@ fun TaskSummaryView(viewModel: TaskSummaryViewModel) {
Text("")
}
task?.group?.assignedDetailsFor(viewModel.userViewModel.userID)?.let {
val formatter = DateFormat.getDateInstance(DateFormat.SHORT)
val formatter = DateFormat.getDateInstance(DateFormat.SHORT, LanguageHelper.systemLocale)
Text(
stringResource(
R.string.assigned_to_you_by,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.habitrpg.android.habitica.models.user.User
import com.habitrpg.android.habitica.ui.adapter.BaseRecyclerViewAdapter
import com.habitrpg.android.habitica.ui.adapter.DiffCallback
import com.habitrpg.android.habitica.ui.viewHolders.ChatRecyclerMessageViewHolder
import com.habitrpg.common.habitica.helpers.LanguageHelper
import java.text.SimpleDateFormat

class ChatDiffCallback(oldList: List<BaseMainObject>, newList: List<BaseMainObject>) :
DiffCallback<ChatMessage>(oldList, newList) {
Expand Down Expand Up @@ -124,7 +126,7 @@ class ChatRecyclerViewAdapter(user: User?, private val isTavern: Boolean) :
}

class SystemChatMessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val dateTime = java.text.SimpleDateFormat.getDateTimeInstance()
private val dateTime = java.text.SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM, LanguageHelper.systemLocale)
val binding = SystemChatMessageBinding.bind(itemView)

var onShouldExpand: (() -> Unit)? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.view.View
import com.habitrpg.android.habitica.helpers.GroupPlanInfoProvider
import com.habitrpg.android.habitica.models.tasks.ChecklistItem
import com.habitrpg.android.habitica.models.tasks.Task
import com.habitrpg.common.habitica.helpers.LanguageHelper
import com.habitrpg.shared.habitica.models.responses.TaskDirection
import java.text.DateFormat
import java.util.Calendar
Expand Down Expand Up @@ -91,7 +92,7 @@ class DailyViewHolder(
companion object {
private val formatter: DateFormat
get() {
return DateFormat.getTimeInstance(DateFormat.SHORT)
return DateFormat.getTimeInstance(DateFormat.SHORT, LanguageHelper.systemLocale)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.habitrpg.android.habitica.R
import com.habitrpg.common.habitica.helpers.LanguageHelper
import java.text.DateFormat
import java.util.Date

val completedTimeFormatToday: DateFormat = DateFormat.getTimeInstance(DateFormat.SHORT)
val completedTimeFormatToday: DateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, LanguageHelper.systemLocale)
val completedTimeFormat: DateFormat =
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, LanguageHelper.systemLocale)

@Composable
fun CompletedAt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.habitrpg.android.habitica.models.tasks.RemindersItem
import com.habitrpg.common.habitica.extensions.dpToPx
import com.habitrpg.common.habitica.extensions.getThemeColor
import com.habitrpg.common.habitica.extensions.layoutInflater
import com.habitrpg.common.habitica.helpers.LanguageHelper
import com.habitrpg.shared.habitica.models.tasks.TaskType
import java.text.DateFormat
import java.time.ZonedDateTime
Expand Down Expand Up @@ -51,12 +52,12 @@ class ReminderItemFormView
private val formatter: DateFormat
get() {
return if (taskType == TaskType.DAILY) {
DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault())
DateFormat.getTimeInstance(DateFormat.SHORT, LanguageHelper.systemLocale)
} else {
DateFormat.getDateTimeInstance(
DateFormat.SHORT,
DateFormat.SHORT,
Locale.getDefault(),
LanguageHelper.systemLocale,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.habitrpg.android.habitica.ui.adapter.SimpleSpinnerAdapter
import com.habitrpg.common.habitica.extensions.dpToPx
import com.habitrpg.common.habitica.extensions.getThemeColor
import com.habitrpg.common.habitica.extensions.layoutInflater
import com.habitrpg.common.habitica.helpers.LanguageHelper
import com.habitrpg.shared.habitica.models.tasks.Frequency
import com.habitrpg.shared.habitica.models.tasks.TaskType
import java.text.DateFormat
Expand All @@ -43,7 +44,7 @@ class TaskSchedulingControls
private val binding = TaskFormTaskSchedulingBinding.inflate(context.layoutInflater, this)
var tintColor: Int = ContextCompat.getColor(context, R.color.brand_300)

private val dateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM)
private val dateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM, LanguageHelper.systemLocale)
private val frequencyAdapter = SimpleSpinnerAdapter(context, R.array.repeatables_frequencies)

var taskType = TaskType.DAILY
Expand All @@ -61,7 +62,7 @@ class TaskSchedulingControls
startDateCalendar.time = value
generateSummary()
}
private var startDateCalendar = Calendar.getInstance()
private var startDateCalendar = Calendar.getInstance(LanguageHelper.systemLocale)
var dueDate: Date? = null
set(value) {
field = value
Expand Down Expand Up @@ -422,7 +423,7 @@ class TaskSchedulingControls
val date = startDateCalendar.get(Calendar.DATE)
val formattedDate =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val formatter = MessageFormat("{0,ordinal}", Locale.getDefault())
val formatter = MessageFormat("{0,ordinal}", LanguageHelper.systemLocale)
formatter.format(arrayOf(date))
} else {
date.toString()
Expand All @@ -432,7 +433,7 @@ class TaskSchedulingControls
val week = startDateCalendar.get(Calendar.WEEK_OF_MONTH)
val formattedWeek =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val formatter = MessageFormat("{0,ordinal}", Locale.getDefault())
val formatter = MessageFormat("{0,ordinal}", LanguageHelper.systemLocale)
formatter.format(arrayOf(week))
} else {
week.toString()
Expand All @@ -441,7 +442,7 @@ class TaskSchedulingControls
startDateCalendar.getDisplayName(
Calendar.DAY_OF_WEEK,
Calendar.LONG,
Locale.getDefault(),
LanguageHelper.systemLocale,
)
" on the $formattedWeek week on $dayLongName"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.habitrpg.common.habitica.helpers

import android.annotation.SuppressLint
import java.util.Locale

class LanguageHelper(languageSharedPref: String?) {
Expand Down Expand Up @@ -38,4 +39,10 @@ class LanguageHelper(languageSharedPref: String?) {
}
}
}

companion object {
// Intentional, we want the system locale, not the app locale
@SuppressLint("ConstantLocale")
val systemLocale = Locale.getAvailableLocales().firstOrNull() ?: Locale.getDefault()
}
}
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
NAME=4.5.0
CODE=8021
CODE=8031

0 comments on commit cca4563

Please sign in to comment.