Skip to content

Commit

Permalink
getDeviceMeasurementSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
fourhtyoz committed Jan 27, 2025
1 parent abfe067 commit aa16d70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
5 changes: 3 additions & 2 deletions store/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { makeAutoObservable, runInAction } from "mobx";
import AsyncStorage from "@react-native-async-storage/async-storage";
import i18n from "@/utils/i18n";
import { getDeviceLanuguage, getDeviceMeasurementSystem } from "@/utils/i18n";


class SettingsStore {
Expand Down Expand Up @@ -65,12 +66,12 @@ class SettingsStore {
break;
case 'language':
runInAction(() => {
this.setLanguage(value || 'en')
this.setLanguage(value || getDeviceLanuguage())
});
break;
case 'units':
runInAction(() => {
this.units = value || 'kg' ;
this.units = value || getDeviceMeasurementSystem() ;
});
break;
case 'notifications':
Expand Down
28 changes: 25 additions & 3 deletions utils/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import * as Localization from 'expo-localization';
import { LANGUAGES } from '@/constants/settings';

// translations
import en from '@/utils/translations/en.json';
Expand All @@ -27,16 +28,37 @@ const resources = {
},
};

const getDeviceLanuguage = () => {
export const getDeviceMeasurementSystem= () => {
try {
const locales = Localization.getLocales()
const measurementSystem = locales[0].measurementSystem
if (measurementSystem === 'metric') {
return 'kg'
} else {
return 'lb'
}
} catch (e) {
console.error(`getDeviceMeasurementSystem error: ${e}`)
return 'kg'
}
};

export const getDeviceLanuguage = () => {
try {
const supportedLanguages = LANGUAGES.map(item => item.code)

const locales = Localization.getLocales()
const languageCode = locales[0].languageCode
return languageCode
if (languageCode && supportedLanguages.includes(languageCode?.toLowerCase())) {
return languageCode
} else {
return 'en'
}
} catch (e) {
console.error(`getDeviceLanuguage error: ${e}`)
return 'en'
}
}
};

i18n
.use(initReactI18next)
Expand Down

0 comments on commit aa16d70

Please sign in to comment.