Skip to content

Commit f29767d

Browse files
committed
feat: add localization() method to Device
Return device locale, language, region, timezone, currency, and preferred language via the Device.GetLocale bridge function as an immutable Localization data object.
1 parent 5235199 commit f29767d

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/Data/Localization.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Native\Mobile\Data;
4+
5+
class Localization
6+
{
7+
public function __construct(
8+
public readonly string $locale,
9+
public readonly string $languageCode,
10+
public readonly string $regionCode,
11+
public readonly string $timezone,
12+
public readonly string $currencyCode,
13+
public readonly string $preferredLanguage,
14+
) {}
15+
}

src/Device.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Native\Mobile;
44

5+
use Native\Mobile\Data\Localization;
6+
57
class Device
68
{
79
public function getId(): ?string
@@ -46,6 +48,28 @@ public function getBatteryInfo(): ?string
4648
return null;
4749
}
4850

51+
public function localization(): ?Localization
52+
{
53+
if (function_exists('nativephp_call')) {
54+
$result = nativephp_call('Device.GetLocale', '{}');
55+
if ($result) {
56+
$decoded = json_decode($result, true);
57+
$info = json_decode($decoded['info'] ?? '{}', true);
58+
59+
return new Localization(
60+
locale: $info['locale'] ?? '',
61+
languageCode: $info['languageCode'] ?? '',
62+
regionCode: $info['regionCode'] ?? '',
63+
timezone: $info['timezone'] ?? '',
64+
currencyCode: $info['currencyCode'] ?? '',
65+
preferredLanguage: $info['preferredLanguage'] ?? '',
66+
);
67+
}
68+
}
69+
70+
return null;
71+
}
72+
4973
/**
5074
* Vibrate the device with a short haptic feedback.
5175
*

src/Facades/Device.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @method static string|null getId()
99
* @method static string|null getInfo()
1010
* @method static string|null getBatteryInfo()
11+
* @method static \Native\Mobile\Data\Localization|null localization()
1112
* @method static bool vibrate()
1213
* @method static array toggleFlashlight()
1314
*/

0 commit comments

Comments
 (0)