@swan-io/shared-business 8.15.14 → 8.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Option } from "@swan-io/boxed";
|
|
2
2
|
import { ValidatorResult } from "@swan-io/use-form";
|
|
3
3
|
import { Except } from "type-fest";
|
|
4
|
+
import { DateFormat } from "../utils/i18n";
|
|
4
5
|
export type MonthNames = readonly [
|
|
5
6
|
string,
|
|
6
7
|
string,
|
|
@@ -34,7 +35,6 @@ export type DatePickerRange = {
|
|
|
34
35
|
start: Option<DatePickerDate>;
|
|
35
36
|
end: Option<DatePickerDate>;
|
|
36
37
|
};
|
|
37
|
-
export type DateFormat = "DD/MM/YYYY" | "MM/DD/YYYY";
|
|
38
38
|
export declare const validateDateRangeOrder: (value: {
|
|
39
39
|
start: string;
|
|
40
40
|
end: string;
|
|
@@ -12,7 +12,8 @@ import { useForm } from "@swan-io/use-form";
|
|
|
12
12
|
import { StyleSheet, View } from "react-native";
|
|
13
13
|
import { match } from "ts-pattern";
|
|
14
14
|
import { extractDate, formatExtractedDate } from "../utils/date";
|
|
15
|
-
import {
|
|
15
|
+
import { t } from "../utils/i18n";
|
|
16
|
+
import { getMostLikelyUserCountry } from "../utils/localization";
|
|
16
17
|
const months = [
|
|
17
18
|
{ value: "01", name: t("datePicker.month.january") },
|
|
18
19
|
{ value: "02", name: t("datePicker.month.february") },
|
|
@@ -50,7 +51,7 @@ const styles = StyleSheet.create({
|
|
|
50
51
|
},
|
|
51
52
|
});
|
|
52
53
|
// https://en.wikipedia.org/wiki/List_of_date_formats_by_country
|
|
53
|
-
const order = match(
|
|
54
|
+
const order = match(getMostLikelyUserCountry().cca2)
|
|
54
55
|
.returnType()
|
|
55
56
|
.with("US", () => "MDY")
|
|
56
57
|
.with("CN", "JP", "KR", "KP", "TW", "HU", "MN", "LT", "BT", () => "YMD")
|
package/src/utils/i18n.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { RifmProps } from "@swan-io/lake/src/utils/rifm";
|
|
2
2
|
import { ReactElement, ReactNode } from "react";
|
|
3
|
-
import type { DateFormat } from "../components/DatePicker";
|
|
4
|
-
import { Country } from "../constants/countries";
|
|
5
3
|
import translationEN from "../locales/en.json";
|
|
4
|
+
export type DateFormat = "DD/MM/YYYY" | "MM/DD/YYYY";
|
|
6
5
|
declare const supportedLanguages: readonly ["en", "es", "de", "fr", "it", "nl", "pt", "fi"];
|
|
7
6
|
type SupportedLanguage = (typeof supportedLanguages)[number];
|
|
8
7
|
type TranslationParams = Record<string, string | number>;
|
|
@@ -23,5 +22,4 @@ export declare const formatNestedMessage: (key: TranslationKey, params: Record<s
|
|
|
23
22
|
export declare const rifmDateProps: RifmProps;
|
|
24
23
|
export declare const isTranslationKey: (value: unknown) => value is TranslationKey;
|
|
25
24
|
export declare const translateError: (error: unknown) => string;
|
|
26
|
-
export declare const getCountry: () => Country;
|
|
27
25
|
export {};
|
package/src/utils/i18n.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createIntl, createIntlCache } from "@formatjs/intl";
|
|
2
|
-
import { isNotNullish } from "@swan-io/lake/src/utils/nullish";
|
|
3
2
|
import { getRifmProps } from "@swan-io/lake/src/utils/rifm";
|
|
4
3
|
import { BadStatusError } from "@swan-io/request";
|
|
5
4
|
import dayjs from "dayjs";
|
|
@@ -17,7 +16,6 @@ import relativeTime from "dayjs/plugin/relativeTime";
|
|
|
17
16
|
import utc from "dayjs/plugin/utc";
|
|
18
17
|
import { cloneElement, isValidElement } from "react";
|
|
19
18
|
import { P, match } from "ts-pattern";
|
|
20
|
-
import { countries, france } from "../constants/countries";
|
|
21
19
|
import translationDE from "../locales/de.json";
|
|
22
20
|
import translationEN from "../locales/en.json";
|
|
23
21
|
import translationES from "../locales/es.json";
|
|
@@ -141,17 +139,3 @@ export const translateError = (error) => {
|
|
|
141
139
|
.otherwise(() => "error.generic");
|
|
142
140
|
return t(isTranslationKey(key) ? key : "error.generic");
|
|
143
141
|
};
|
|
144
|
-
export const getCountry = () => {
|
|
145
|
-
const navigatorCountries = navigator.languages
|
|
146
|
-
.map(language => language.split("-")[1])
|
|
147
|
-
.filter(isNotNullish);
|
|
148
|
-
for (let index = 0; index < navigatorCountries.length; index++) {
|
|
149
|
-
const navigatorCountry = navigatorCountries[index];
|
|
150
|
-
const country = countries.find(({ cca2 }) => cca2 === navigatorCountry);
|
|
151
|
-
if (isNotNullish(country)) {
|
|
152
|
-
return country;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
// fallback to france when no valid country found in navigator locales
|
|
156
|
-
return france;
|
|
157
|
-
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isNotNullish } from "@swan-io/lake/src/utils/nullish";
|
|
2
|
+
import { countries, france } from "../constants/countries";
|
|
3
|
+
export const getMostLikelyUserCountry = () => {
|
|
4
|
+
const navigatorCountries = navigator.languages
|
|
5
|
+
.map(language => language.split("-")[1])
|
|
6
|
+
.filter(isNotNullish);
|
|
7
|
+
for (let index = 0; index < navigatorCountries.length; index++) {
|
|
8
|
+
const navigatorCountry = navigatorCountries[index];
|
|
9
|
+
const country = countries.find(({ cca2 }) => cca2 === navigatorCountry);
|
|
10
|
+
if (isNotNullish(country)) {
|
|
11
|
+
return country;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
// fallback to france when no valid country found in navigator locales
|
|
15
|
+
return france;
|
|
16
|
+
};
|