Skip to content

Commit

Permalink
Feat: Add locale to device context and deprecate language (#1832)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Dec 9, 2021
1 parent 5ce8406 commit b182c6e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Ref: Rename Fragment span operation from `ui.fragment.load` to `ui.load` (#1824)
* Feat: Add locale to device context and deprecate language (#1832)
* Ref: change `java.util.Random` to `java.security.SecureRandom` for possible security reasons (#1831)

## 5.4.3
Expand Down
Expand Up @@ -310,8 +310,13 @@ private void setArchitectures(final @NotNull Device device) {
if (device.getId() == null) {
device.setId(getDeviceId());
}

final Locale locale = Locale.getDefault();
if (device.getLanguage() == null) {
device.setLanguage(Locale.getDefault().toString()); // eg en_US
device.setLanguage(locale.getLanguage());
}
if (device.getLocale() == null) {
device.setLocale(locale.toString()); // eg en_US
}

return device;
Expand Down
Expand Up @@ -27,6 +27,7 @@ import io.sentry.protocol.SentryTransaction
import io.sentry.protocol.User
import io.sentry.test.getCtor
import org.junit.runner.RunWith
import java.util.Locale
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -44,6 +45,10 @@ class DefaultAndroidEventProcessorTest {
private val className = "io.sentry.android.core.DefaultAndroidEventProcessor"
private val ctorTypes = arrayOf(Context::class.java, ILogger::class.java, IBuildInfoProvider::class.java)

init {
Locale.setDefault(Locale.US)
}

private class Fixture {
val buildInfo = mock<IBuildInfoProvider>()
val options = SentryOptions().apply {
Expand Down Expand Up @@ -376,4 +381,15 @@ class DefaultAndroidEventProcessorTest {
// assertNotNull(device.connectionType)
}
}

@Test
fun `Event sets language and locale`() {
val sut = fixture.getSut(context)

assertNotNull(sut.process(SentryEvent(), null)) {
val device = it.contexts.device!!
assertEquals("en", device.language)
assertEquals("en_US", device.locale)
}
}
}
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Expand Up @@ -1532,6 +1532,7 @@ public final class io/sentry/protocol/Device : io/sentry/IUnknownPropertiesConsu
public fun getFreeStorage ()Ljava/lang/Long;
public fun getId ()Ljava/lang/String;
public fun getLanguage ()Ljava/lang/String;
public fun getLocale ()Ljava/lang/String;
public fun getManufacturer ()Ljava/lang/String;
public fun getMemorySize ()Ljava/lang/Long;
public fun getModel ()Ljava/lang/String;
Expand Down Expand Up @@ -1563,6 +1564,7 @@ public final class io/sentry/protocol/Device : io/sentry/IUnknownPropertiesConsu
public fun setFreeStorage (Ljava/lang/Long;)V
public fun setId (Ljava/lang/String;)V
public fun setLanguage (Ljava/lang/String;)V
public fun setLocale (Ljava/lang/String;)V
public fun setLowMemory (Ljava/lang/Boolean;)V
public fun setManufacturer (Ljava/lang/String;)V
public fun setMemorySize (Ljava/lang/Long;)V
Expand Down
22 changes: 21 additions & 1 deletion sentry/src/main/java/io/sentry/protocol/Device.java
Expand Up @@ -93,7 +93,18 @@ public final class Device implements IUnknownPropertiesConsumer {
private @Nullable TimeZone timezone;

private @Nullable String id;
private @Nullable String language;

/**
* This method returns the language code for this locale, which will either be the empty string or
* a lowercase ISO 639 code.
*
* @deprecated use {@link Device#getLocale()}
*/
@Deprecated private @Nullable String language;

/** The locale of the device. For example, en-US. */
private @Nullable String locale;

private @Nullable String connectionType;

/** battery's temperature in celsius */
Expand Down Expand Up @@ -135,6 +146,7 @@ public Device() {}
this.batteryLevel = device.batteryLevel;
final String[] archsRef = device.archs;
this.archs = archsRef != null ? archsRef.clone() : null;
this.locale = device.locale;

final TimeZone timezoneRef = device.timezone;
this.timezone = timezoneRef != null ? (TimeZone) timezoneRef.clone() : null;
Expand Down Expand Up @@ -384,6 +396,14 @@ public void setBatteryTemperature(final @Nullable Float batteryTemperature) {
this.batteryTemperature = batteryTemperature;
}

public @Nullable String getLocale() {
return locale;
}

public void setLocale(final @Nullable String locale) {
this.locale = locale;
}

@TestOnly
@Nullable
Map<String, Object> getUnknown() {
Expand Down

0 comments on commit b182c6e

Please sign in to comment.