Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ $result = Device::getInfo();
// Get battery info
$result = Device::getBatteryInfo();
// Returns: ['info' => '{"batteryLevel":0.85,"isCharging":false}']

// Get localization info
$localization = Device::localization();
// Returns DeviceLocale object: $localization->locale, $localization->languageCode, etc.
```

### JavaScript (Vue/React/Inertia)
Expand Down Expand Up @@ -64,6 +68,12 @@ console.log('Platform:', info.platform);
const batteryResult = await Device.getBatteryInfo();
const battery = JSON.parse(batteryResult.info);
console.log('Battery level:', battery.batteryLevel * 100 + '%');

// Get localization info
const localeResult = await Device.getLocale();
const localization = JSON.parse(localeResult.info);
console.log('Language:', localization.languageCode);
console.log('Timezone:', localization.timezone);
```

## Methods
Expand Down Expand Up @@ -116,6 +126,20 @@ Battery info includes:
- `batteryLevel` - Battery level from 0.0 to 1.0
- `isCharging` - Whether device is charging

### `localization(): DeviceLocale`

Get device locale and regional settings.

**Returns:** `DeviceLocale` object (PHP) / `{ info: string }` JSON string (JS)

Localization info includes:
- `locale` - Full locale identifier (e.g., "en_GB")
- `languageCode` - Language code (e.g., "en")
- `regionCode` - Region/country code (e.g., "GB")
- `timezone` - Timezone identifier (e.g., "America/New_York")
- `currencyCode` - Currency code (e.g., "GBP")
- `preferredLanguage` - User's preferred language (e.g., "en-GB")

## Permissions

### Android
Expand Down
6 changes: 6 additions & 0 deletions nativephp.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
"android": "com.nativephp.device.DeviceFunctions.GetBatteryInfo",
"ios": "DeviceFunctions.GetBatteryInfo",
"description": "Get battery level and charging status"
},
{
"name": "Device.GetLocale",
"android": "com.nativephp.device.DeviceFunctions.GetLocale",
"ios": "DeviceFunctions.GetLocale",
"description": "Get device locale and regional settings"
}
],

Expand Down
50 changes: 50 additions & 0 deletions resources/android/DeviceFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.hardware.camera2.CameraManager
import android.os.BatteryManager
import android.os.Build
import android.os.Debug
import android.os.LocaleList
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
Expand Down Expand Up @@ -276,4 +277,53 @@ object DeviceFunctions {
}
}
}

/**
* Get device locale and regional settings
* Parameters: none
* Returns:
* - info: JSON string with locale, language, region, timezone, currency, and preferred language
*/
class GetLocale(private val context: Context) : BridgeFunction {
override fun execute(parameters: Map<String, Any>): Map<String, Any> {
Log.d("Device.GetLocale", "Getting locale info")

return try {
val locale = java.util.Locale.getDefault()

val currencyCode = try {
java.util.Currency.getInstance(locale).currencyCode
} catch (e: Exception) {
""
}

val preferredLanguage = try {
val localeList = LocaleList.getDefault()
if (localeList.size() > 0) {
localeList.get(0).toLanguageTag()
} else {
locale.language
}
} catch (e: Exception) {
locale.language
}

val localeInfo = JSONObject().apply {
put("locale", locale.toString())
put("languageCode", locale.language)
put("regionCode", locale.country)
put("timezone", java.util.TimeZone.getDefault().id)
put("currencyCode", currencyCode)
put("preferredLanguage", preferredLanguage)
}

val result = localeInfo.toString()
Log.d("Device.GetLocale", "Locale info retrieved")
mapOf("info" to result)
} catch (e: Exception) {
Log.e("Device.GetLocale", "Error: ${e.message}", e)
mapOf("info" to "{\"error\": \"${e.message}\"}")
}
}
}
}
26 changes: 25 additions & 1 deletion resources/boost/guidelines/core.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
// Get battery info
$batteryInfo = Device::getBatteryInfo();
// batteryLevel: 0-1 (e.g., 0.85 = 85%), isCharging: true/false

// Get localization info
$localization = Device::localization();
// $localization->locale, $localization->languageCode, $localization->timezone, etc.
</code-snippet>
@endverbatim

Expand Down Expand Up @@ -58,6 +62,15 @@
const battery = JSON.parse(batteryResult.info);
console.log(batteryResult.batteryLevel); // 0-1
console.log(batteryResult.isCharging); // true/false

// Get localization info
const localeResult = await device.getLocale();
const localization = JSON.parse(localeResult.info);
console.log(localization.locale); // e.g., 'en_GB'
console.log(localization.languageCode); // e.g., 'en'
console.log(localization.timezone); // e.g., 'America/New_York'
console.log(localization.currencyCode); // e.g., 'GBP'
console.log(localization.preferredLanguage); // e.g., 'en-GB'
</code-snippet>
@endverbatim

Expand All @@ -78,4 +91,15 @@
| Property | Type | Description |
|----------|------|-------------|
| batteryLevel | number | Charge percentage (0-1) |
| isCharging | boolean | Current charging status |
| isCharging | boolean | Current charging status |

### Localization Properties

| Property | Type | Description |
|----------|------|-------------|
| locale | string | Full locale identifier (e.g., en_GB) |
| languageCode | string | Language code (e.g., en) |
| regionCode | string | Region/country code (e.g., GB) |
| timezone | string | Timezone identifier (e.g., America/New_York) |
| currencyCode | string | Currency code (e.g., GBP) |
| preferredLanguage | string | User's preferred language (e.g., en-GB) |
44 changes: 44 additions & 0 deletions resources/ios/DeviceFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,48 @@ enum DeviceFunctions {
}
}
}

// MARK: - Device.GetLocale

/// Get device locale and regional settings
/// Parameters: none
/// Returns:
/// - info: JSON string with locale, language, region, timezone, currency, and preferred language
class GetLocale: BridgeFunction {
func execute(parameters: [String: Any]) throws -> [String: Any] {
print("Device.GetLocale called")

let current = Locale.current

let localeIdentifier = current.identifier
let languageCode = current.language.languageCode?.identifier ?? ""
let regionCode = current.region?.identifier ?? ""
let timezone = TimeZone.current.identifier
let currencyCode = current.currency?.identifier ?? ""
let preferredLanguage = Locale.preferredLanguages.first ?? (current.language.languageCode?.identifier ?? "")

let localeInfo: [String: Any] = [
"locale": localeIdentifier,
"languageCode": languageCode,
"regionCode": regionCode,
"timezone": timezone,
"currencyCode": currencyCode,
"preferredLanguage": preferredLanguage
]

print("Locale info collected")

do {
let jsonData = try JSONSerialization.data(withJSONObject: localeInfo, options: [])
if let jsonString = String(data: jsonData, encoding: .utf8) {
return ["info": jsonString]
} else {
return ["info": "{\"error\": \"Failed to convert JSON data to string\"}"]
}
} catch {
print("Error serializing locale info: \(error)")
return ["info": "{\"error\": \"\(error.localizedDescription)\"}"]
}
}
}
}