forked from andreykobal/Amber-mint-page
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseLocales.ts
137 lines (132 loc) · 3.36 KB
/
useLocales.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { useStaticQuery, graphql } from "gatsby"
import { useLocation } from "@reach/router"
import type { AllLocalesQuery } from "../../graphql-types"
type I18n = NonNullable<
AllLocalesQuery["allFile"]["nodes"][number]["childI18NJson"]
>
export type Locale = {
id: string
} & {
[K in keyof I18n]: NonNullable<I18n[K]>
}
/**
* Gatsby really wants to push everything, even simple stuff like JSON files in
* a project folder, through a complicated GraphQL pipeline. This hook hides the
* details of looking up the locale files in the `i18n` folder and makes them
* easily accessible to any component that needs them.
*
* @returns the list of all `locales`, as well as the current `locale` given by the URL
*/
export default function useLocales(): { locales: Locale[]; locale?: Locale } {
const { allFile }: AllLocalesQuery = useStaticQuery(
graphql`
query AllLocales {
allFile(
filter: {
sourceInstanceName: { eq: "i18n" }
extension: { eq: "json" }
}
) {
nodes {
name
childI18NJson {
viewIn
langPicker
siteTitle
title
login
description
mint3DNFT
billedOnce
whitepaper
tokenomics
profile
marketplace
contactus
recentlyMinted
mintDescription
calendarEvent
connectWallet
signOut
new
myNFTs
nextNFT
prevNFT
close
zoomIn
zoomOut
mint
play
notLoggedIn
continueAsGuest
download
mintAvatar
item
total
near
congratulation
youritem
successMinted
share
sorry
sorryDesc
goBack
search
playAmberGame
getFullownership
receivingRareNFT
year
months
month
obtainStatus
becomeMember
recentlyMintedDesc
rarity
getNFTUnique
getNFTDesc
participateGame
gameDesc
trade
tradeDesc
tradeSmallDesc
rule1
rule2
rule3
rule4
rule5
rule6
rule7
rule8
instruct1
instruct2
instruct3
instruct4
instruct5
instruct6
instruct7
instruct8
instruct9
instruct10
instruct11
partners
joinUs
copywrite
about
vote
}
}
}
}
`
)
const { pathname } = useLocation()
const locales = allFile.nodes.map(
node =>
({
id: node.name,
...node.childI18NJson!,
} as Locale)
) // type coercion removes the `| null` that GraphQL includes
const locale = locales.find(l => new RegExp(`/${l.id}`).test(pathname))
return { locales, locale }
}