-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #324 from CodeRedDev/feature/per-app-language
Per App Language
- Loading branch information
Showing
10 changed files
with
304 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
app/src/main/java/com/pr0gramm/app/ui/dialogs/LanguagePickerDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package com.pr0gramm.app.ui.dialogs | ||
|
||
import android.os.Build | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.appcompat.app.AppCompatDelegate | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.wrapContentHeight | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.ButtonDefaults.textButtonColors | ||
import androidx.compose.material.Card | ||
import androidx.compose.material.ExperimentalMaterialApi | ||
import androidx.compose.material.ListItem | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.ProvideTextStyle | ||
import androidx.compose.material.RadioButton | ||
import androidx.compose.material.RadioButtonDefaults | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.res.colorResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.Dialog | ||
import androidx.core.os.LocaleListCompat | ||
import com.pr0gramm.app.R | ||
import com.pr0gramm.app.ui.base.BaseDialogFragment | ||
import org.xmlpull.v1.XmlPullParser | ||
import java.util.Locale | ||
|
||
class LanguagePickerDialog : BaseDialogFragment("LanguagePickerDialog") { | ||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
val supportedLocales = getSupportedLocales() | ||
val currentLocale = getCurrentLocale(supportedLocales) | ||
|
||
return ComposeView(requireContext()).apply { | ||
setContent { | ||
DialogContent(supportedLocales, currentLocale) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun DialogContent( | ||
supportedLocales: List<Locale>, | ||
initialLocale: Locale | ||
) { | ||
val selectedLocale = remember { mutableStateOf(initialLocale) } | ||
|
||
Dialog( | ||
onDismissRequest = { dismiss() } | ||
) { | ||
Card( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.wrapContentHeight() | ||
.padding(16.dp), | ||
shape = RoundedCornerShape(16.dp), | ||
) { | ||
val titleStyle = MaterialTheme.typography.subtitle1 | ||
val subtitleStyle = MaterialTheme.typography.body2 | ||
Column { | ||
ProvideTextStyle(titleStyle) { | ||
Text( | ||
stringResource(R.string.language_picker_title), | ||
Modifier | ||
.padding(start = 16.dp, end = 16.dp, top = 16.dp) | ||
.align(Alignment.Start), | ||
) | ||
} | ||
ProvideTextStyle(subtitleStyle) { | ||
Text( | ||
stringResource(R.string.language_picker_subtitle), | ||
Modifier | ||
.padding(vertical = 4.dp, horizontal = 16.dp) | ||
.align(Alignment.Start), | ||
) | ||
} | ||
supportedLocales.forEach { | ||
LanguageItem( | ||
it, | ||
selected = it == selectedLocale.value, | ||
onClick = { | ||
selectedLocale.value = it | ||
}, | ||
) | ||
} | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth(), | ||
horizontalArrangement = Arrangement.End, | ||
) { | ||
TextButton( | ||
onClick = { dismiss() }, | ||
modifier = Modifier.padding(8.dp), | ||
colors = textButtonColors( | ||
contentColor = colorResource(R.color.orange_primary) | ||
), | ||
) { | ||
Text(stringResource(R.string.language_picker_dismiss)) | ||
} | ||
TextButton( | ||
onClick = { | ||
AppCompatDelegate.setApplicationLocales( | ||
LocaleListCompat.create( | ||
selectedLocale.value | ||
) | ||
) | ||
dismiss() | ||
}, | ||
modifier = Modifier.padding(8.dp), | ||
colors = textButtonColors( | ||
contentColor = colorResource(R.color.orange_primary) | ||
), | ||
) { | ||
Text(stringResource(R.string.language_picker_confirm)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalMaterialApi::class) | ||
@Composable | ||
fun LanguageItem( | ||
locale: Locale, | ||
selected: Boolean, | ||
onClick: (() -> Unit), | ||
) { | ||
ListItem( | ||
modifier = Modifier.clickable { | ||
onClick() | ||
}, | ||
text = { Text(locale.getDisplayLanguage(locale)) }, | ||
trailing = { | ||
RadioButton( | ||
selected = selected, | ||
onClick = onClick, | ||
colors = RadioButtonDefaults.colors( | ||
selectedColor = colorResource(R.color.orange_primary) | ||
) | ||
) | ||
} | ||
) | ||
} | ||
|
||
private fun getSupportedLocales(): List<Locale> { | ||
val locales = mutableListOf<Locale>() | ||
|
||
try { | ||
val parser = resources.getXml(R.xml.locales_config) | ||
var eventType = parser.eventType | ||
val namespace = "http://schemas.android.com/apk/res/android" | ||
|
||
while (eventType != XmlPullParser.END_DOCUMENT) { | ||
if (eventType == XmlPullParser.START_TAG && parser.name == "locale") { | ||
val languageTag = parser.getAttributeValue(namespace, "name") | ||
if (languageTag != null) { | ||
val locale = Locale.forLanguageTag(languageTag) | ||
locales.add(locale) | ||
} | ||
} | ||
eventType = parser.next() | ||
} | ||
|
||
parser.close() | ||
} catch (e: Exception) { | ||
logger.error("Error parsing locales config!", e) | ||
} | ||
|
||
return locales | ||
} | ||
|
||
private fun getCurrentLocale(supportedLocales: List<Locale>): Locale { | ||
val applicationLocales = AppCompatDelegate.getApplicationLocales() | ||
val appLocale = if (!applicationLocales.isEmpty) { | ||
applicationLocales.get(0)!! | ||
} else { | ||
getCurrentAppLocale() | ||
} | ||
|
||
val candidate = supportedLocales.firstOrNull { it.isO3Language == appLocale.isO3Language } | ||
|
||
if (candidate != null) { | ||
return candidate | ||
} | ||
|
||
return Locale.forLanguageTag("en-US") | ||
} | ||
|
||
private fun getCurrentAppLocale(): Locale { | ||
val config = requireContext().resources.configuration | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
config.locales[0] | ||
} else { | ||
@Suppress("DEPRECATION") | ||
config.locale | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#FFFFFF" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp"> | ||
|
||
<path android:fillColor="@android:color/white" android:pathData="M12.87,15.07l-2.54,-2.51 0.03,-0.03c1.74,-1.94 2.98,-4.17 3.71,-6.53L17,6L17,4h-7L10,2L8,2v2L1,4v1.99h11.17C11.5,7.92 10.44,9.75 9,11.35 8.07,10.32 7.3,9.19 6.69,8h-2c0.73,1.63 1.73,3.17 2.98,4.56l-5.09,5.02L4,19l5,-5 3.11,3.11 0.76,-2.04zM18.5,10h-2L12,22h2l1.12,-3h4.75L21,22h2l-4.5,-12zM15.88,17l1.62,-4.33L19.12,17h-3.24z"/> | ||
|
||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<locale-config xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<locale android:name="en-US"/> | ||
<locale android:name="de-DE"/> | ||
</locale-config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters