or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

device-preferences.mddevice-settings.mdindex.mdlanguage-matching.mdnumber-currency-formatting.mdplatform-utilities.md

device-preferences.mddocs/

0

# Device Preferences

1

2

System preferences for measurement units, time format, and automatic settings to help apps adapt to user preferences and regional conventions.

3

4

## Capabilities

5

6

### Get Temperature Unit

7

8

Gets the device's preferred temperature unit based on regional settings.

9

10

```typescript { .api }

11

/**

12

* Get device's temperature unit preference

13

* @returns "celsius" for metric regions, "fahrenheit" for imperial regions

14

*/

15

function getTemperatureUnit(): TemperatureUnit;

16

17

type TemperatureUnit = "celsius" | "fahrenheit";

18

```

19

20

**Usage Examples:**

21

22

```typescript

23

import { getTemperatureUnit } from "react-native-localize";

24

25

const tempUnit = getTemperatureUnit();

26

console.log(tempUnit); // "celsius" or "fahrenheit"

27

28

// Temperature display logic

29

function formatTemperature(celsius: number): string {

30

const unit = getTemperatureUnit();

31

32

if (unit === "fahrenheit") {

33

const fahrenheit = (celsius * 9/5) + 32;

34

return `${Math.round(fahrenheit)}°F`;

35

} else {

36

return `${Math.round(celsius)}°C`;

37

}

38

}

39

40

console.log(formatTemperature(25));

41

// Celsius regions: "25°C"

42

// Fahrenheit regions: "77°F"

43

```

44

45

**Weather App Integration:**

46

47

```typescript

48

import { getTemperatureUnit } from "react-native-localize";

49

50

class WeatherService {

51

getTemperatureDisplay(celsiusTemp: number): { value: number; unit: string } {

52

const unit = getTemperatureUnit();

53

54

if (unit === "fahrenheit") {

55

return {

56

value: Math.round((celsiusTemp * 9/5) + 32),

57

unit: "°F"

58

};

59

} else {

60

return {

61

value: Math.round(celsiusTemp),

62

unit: "°C"

63

};

64

}

65

}

66

}

67

```

68

69

### Check 24-Hour Clock Usage

70

71

Determines if the device uses 24-hour time format.

72

73

```typescript { .api }

74

/**

75

* Check if device uses 24-hour clock format

76

* @returns true if 24-hour format, false if 12-hour format (AM/PM)

77

*/

78

function uses24HourClock(): boolean;

79

```

80

81

**Usage Examples:**

82

83

```typescript

84

import { uses24HourClock } from "react-native-localize";

85

86

const is24Hour = uses24HourClock();

87

console.log(is24Hour); // true or false

88

89

// Time formatting based on device preference

90

function formatTime(date: Date): string {

91

const is24Hour = uses24HourClock();

92

93

return date.toLocaleTimeString(undefined, {

94

hour12: !is24Hour,

95

hour: 'numeric',

96

minute: '2-digit'

97

});

98

}

99

100

const now = new Date();

101

console.log(formatTime(now));

102

// 24-hour regions: "14:30"

103

// 12-hour regions: "2:30 PM"

104

```

105

106

**Clock Component Example:**

107

108

```typescript

109

import { uses24HourClock } from "react-native-localize";

110

111

function DigitalClock() {

112

const [time, setTime] = useState(new Date());

113

const is24Hour = uses24HourClock();

114

115

useEffect(() => {

116

const timer = setInterval(() => setTime(new Date()), 1000);

117

return () => clearInterval(timer);

118

}, []);

119

120

const timeString = time.toLocaleTimeString(undefined, {

121

hour12: !is24Hour,

122

hour: 'numeric',

123

minute: '2-digit',

124

second: '2-digit'

125

});

126

127

return <Text>{timeString}</Text>;

128

}

129

```

130

131

### Check Metric System Usage

132

133

Determines if the device uses the metric measurement system.

134

135

```typescript { .api }

136

/**

137

* Check if device uses metric system

138

* @returns true for metric system, false for imperial system

139

*/

140

function usesMetricSystem(): boolean;

141

```

142

143

**Usage Examples:**

144

145

```typescript

146

import { usesMetricSystem } from "react-native-localize";

147

148

const isMetric = usesMetricSystem();

149

console.log(isMetric); // true for most countries, false for US, Liberia, Myanmar

150

151

// Distance formatting

152

function formatDistance(meters: number): string {

153

const isMetric = usesMetricSystem();

154

155

if (isMetric) {

156

if (meters < 1000) {

157

return `${Math.round(meters)} m`;

158

} else {

159

return `${(meters / 1000).toFixed(1)} km`;

160

}

161

} else {

162

const feet = meters * 3.28084;

163

if (feet < 5280) {

164

return `${Math.round(feet)} ft`;

165

} else {

166

const miles = feet / 5280;

167

return `${miles.toFixed(1)} mi`;

168

}

169

}

170

}

171

172

console.log(formatDistance(1500));

173

// Metric: "1.5 km"

174

// Imperial: "0.9 mi"

175

```

176

177

**Fitness App Integration:**

178

179

```typescript

180

import { usesMetricSystem } from "react-native-localize";

181

182

class FitnessMetrics {

183

formatWeight(kg: number): { value: number; unit: string } {

184

if (usesMetricSystem()) {

185

return { value: Math.round(kg * 10) / 10, unit: "kg" };

186

} else {

187

const pounds = kg * 2.20462;

188

return { value: Math.round(pounds * 10) / 10, unit: "lbs" };

189

}

190

}

191

192

formatHeight(cm: number): { value: string; unit: string } {

193

if (usesMetricSystem()) {

194

return { value: cm.toString(), unit: "cm" };

195

} else {

196

const totalInches = cm / 2.54;

197

const feet = Math.floor(totalInches / 12);

198

const inches = Math.round(totalInches % 12);

199

return { value: `${feet}'${inches}"`, unit: "" };

200

}

201

}

202

}

203

```

204

205

### Check Auto Date and Time

206

207

Checks if the device has automatic date and time setting enabled.

208

209

```typescript { .api }

210

/**

211

* Check if device uses automatic date and time

212

* @returns true if enabled, false if disabled, undefined if not available/determinable

213

*/

214

function usesAutoDateAndTime(): boolean | undefined;

215

```

216

217

**Usage Examples:**

218

219

```typescript

220

import { usesAutoDateAndTime } from "react-native-localize";

221

222

const autoDateTime = usesAutoDateAndTime();

223

224

if (autoDateTime === true) {

225

console.log("Device automatically syncs date/time");

226

} else if (autoDateTime === false) {

227

console.log("Manual date/time setting");

228

// Maybe show a warning about potential time sync issues

229

} else {

230

console.log("Auto date/time status unknown");

231

}

232

```

233

234

### Check Auto Timezone

235

236

Checks if the device has automatic timezone setting enabled.

237

238

```typescript { .api }

239

/**

240

* Check if device uses automatic timezone

241

* @returns true if enabled, false if disabled, undefined if not available/determinable

242

*/

243

function usesAutoTimeZone(): boolean | undefined;

244

```

245

246

**Usage Examples:**

247

248

```typescript

249

import { usesAutoTimeZone } from "react-native-localize";

250

251

const autoTimezone = usesAutoTimeZone();

252

253

function checkTimeSettings() {

254

const autoTz = usesAutoTimeZone();

255

256

if (autoTz === false) {

257

// Warn user about potential timezone issues

258

return {

259

warning: "Manual timezone detected. Location-based features may be inaccurate.",

260

suggestion: "Enable automatic timezone in device settings"

261

};

262

}

263

264

return { status: "ok" };

265

}

266

```

267

268

**Complete Device Preferences Check:**

269

270

```typescript

271

import {

272

getTemperatureUnit,

273

uses24HourClock,

274

usesMetricSystem,

275

usesAutoDateAndTime,

276

usesAutoTimeZone

277

} from "react-native-localize";

278

279

function getDevicePreferences() {

280

return {

281

temperature: getTemperatureUnit(),

282

timeFormat: uses24HourClock() ? "24h" : "12h",

283

measurements: usesMetricSystem() ? "metric" : "imperial",

284

autoDateTime: usesAutoDateAndTime(),

285

autoTimezone: usesAutoTimeZone()

286

};

287

}

288

289

const prefs = getDevicePreferences();

290

console.log(prefs);

291

// {

292

// temperature: "celsius",

293

// timeFormat: "24h",

294

// measurements: "metric",

295

// autoDateTime: true,

296

// autoTimezone: true

297

// }

298

299

// Use preferences to configure app defaults

300

function configureAppDefaults(preferences: typeof prefs) {

301

// Set temperature unit for weather displays

302

setTemperatureUnit(preferences.temperature);

303

304

// Configure time pickers and displays

305

setTimeFormat(preferences.timeFormat);

306

307

// Set distance and weight units

308

setMeasurementSystem(preferences.measurements);

309

310

// Show warnings for manual time settings if needed

311

if (preferences.autoDateTime === false || preferences.autoTimezone === false) {

312

showTimeSettingsWarning();

313

}

314

}

315

```