zoontek / react-native-localize

🌍 A toolbox for your React Native app localization
MIT License
2.28k stars 212 forks source link

How to get first day of the week in user region? #88

Closed pie6k closed 4 years ago

pie6k commented 4 years ago

Some countries use Sunday, Monday, or even other days http://chartsbin.com/view/41671

Is it possible to get it using this lib?

zoontek commented 4 years ago

@pie6k Hi. Totally, yes (even if it's not supported yet). If we refer to http://cldr.unicode.org latest release, we found in supplementalData.xml these infos:

<firstDay day="mon" territories="001 AD AI AL AM AN AR AT AX AZ BA BE BG BM BN BY CH CL CM CR CY CZ DE DK EC EE ES FI FJ FO FR GB GE GF GP GR HR HU IE IS IT KG KZ LB LI LK LT LU LV MC MD ME MK MN MQ MY NL NO NZ PL RE RO RS RU SE SI SK SM TJ TM TR UA UY UZ VA VN XK" />
<firstDay day="fri" territories="MV" />
<firstDay day="sat" territories="AE AF BH DJ DZ EG IQ IR JO KW LY OM QA SD SY" />
<firstDay day="sun" territories="AG AS AU BD BR BS BT BW BZ CA CN CO DM DO ET GB GT GU HK HN ID IL IN JM JP KE KH KR LA MH MM MO MT MX MZ NI NP PA PE PH PK PR PT PY SA SG SV TH TT TW UM US VE VI WS YE ZA ZW" />

As 001 means regions not explicitly listed, we can simplify it to:

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

const startsOnFriday = ["MV"];

const startsOnSaturday = [
  "AE",
  "AF",
  "BH",
  "DJ",
  "DZ",
  "EG",
  "IQ",
  "IR",
  "JO",
  "KW",
  "LY",
  "OM",
  "QA",
  "SD",
  "SY",
];

const startsOnSunday = [
  "AG",
  "AS",
  "AU",
  "BD",
  "BR",
  "BS",
  "BT",
  "BW",
  "BZ",
  "CA",
  "CN",
  "CO",
  "DM",
  "DO",
  "ET",
  "GB",
  "GT",
  "GU",
  "HK",
  "HN",
  "ID",
  "IL",
  "IN",
  "JM",
  "JP",
  "KE",
  "KH",
  "KR",
  "LA",
  "MH",
  "MM",
  "MO",
  "MT",
  "MX",
  "MZ",
  "NI",
  "NP",
  "PA",
  "PE",
  "PH",
  "PK",
  "PR",
  "PT",
  "PY",
  "SA",
  "SG",
  "SV",
  "TH",
  "TT",
  "TW",
  "UM",
  "US",
  "VE",
  "VI",
  "WS",
  "YE",
  "ZA",
  "ZW",
];

function getFirstDayOfWeek() {
  const country = getCountry();

  if (startsOnFriday.includes(country)) {
    return "friday";
  }
  if (startsOnSaturday.includes(country)) {
    return "saturday";
  }
  if (startsOnSunday.includes(country)) {
    return "sunday";
  }

  return "monday";
}

A bit roots but it's works! Might seems fat but is actually only 653 bytes minified (without gzip) 🙂

MoOx commented 4 years ago

I am interested to have this too :)

robrechtme commented 2 years ago

On iOS there's now a separate setting for this, so looking at the country won't always be correct. Is there a way to expose this setting? 🙂

zoontek commented 2 years ago

@robrechtme There is indeed room for a new feature here (with fallback to the list above)

tinkench commented 1 year ago

@zoontek Two countries have changed they first day of the week since your comment. Here is the updated version:

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

const startsOnFriday = ["MV"];

const startsOnSaturday = [
  "AE",
  "AF",
  "BH",
  "DJ",
  "DZ",
  "EG",
  "IQ",
  "IR",
  "JO",
  "KW",
  "LY",
  "OM",
  "QA",
  "SD",
  "SY",
];

const startsOnSunday = [
  "AG",
  "AS",
  "BD",
  "BR",
  "BS",
  "BT",
  "BW",
  "BZ",
  "CA",
  "CO",
  "DM",
  "DO",
  "ET",
  "GB",
  "GT",
  "GU",
  "HK",
  "HN",
  "ID",
  "IL",
  "IN",
  "JM",
  "JP",
  "KE",
  "KH",
  "KR",
  "LA",
  "MH",
  "MM",
  "MO",
  "MT",
  "MX",
  "MZ",
  "NI",
  "NP",
  "PA",
  "PE",
  "PH",
  "PK",
  "PR",
  "PT",
  "PY",
  "SA",
  "SG",
  "SV",
  "TH",
  "TT",
  "TW",
  "UM",
  "US",
  "VE",
  "VI",
  "WS",
  "YE",
  "ZA",
  "ZW",
];

function getFirstDayOfWeek() {
  const country = getCountry();

  if (startsOnFriday.includes(country)) {
    return "friday";
  }
  if (startsOnSaturday.includes(country)) {
    return "saturday";
  }
  if (startsOnSunday.includes(country)) {
    return "sunday";
  }

  return "monday";
}
chenei commented 9 months ago

@zoontek sorry for bringing this up, there is a new option on iOS to choose whatever first day of the week you want, is there a way you support that?