@@ -106,62 +106,64 @@ No setup headaches. Just clean, context-aware translations, out of the box.
106106
107107## API
108108
109- ### getLocales ()
109+ ### getCalendar ()
110110
111- Returns the user preferred locales, in order .
111+ Returns the user preferred calendar format .
112112
113113#### Method type
114114
115115``` ts
116- type getLocales = () => Array <{
117- languageCode: string ;
118- scriptCode? : string ;
119- countryCode: string ;
120- languageTag: string ;
121- isRTL: boolean ;
122- }>;
116+ type getCalendar = () =>
117+ | " gregorian"
118+ | " buddhist"
119+ | " coptic"
120+ | " ethiopic"
121+ | " ethiopic-amete-alem"
122+ | " hebrew"
123+ | " indian"
124+ | " islamic"
125+ | " islamic-umm-al-qura"
126+ | " islamic-civil"
127+ | " islamic-tabular"
128+ | " iso8601"
129+ | " japanese"
130+ | " persian" ;
123131```
124132
125133#### Usage example
126134
127135``` ts
128- import { getLocales } from " react-native-localize" ;
136+ import { getCalendar } from " react-native-localize" ;
129137
130- console .log (getLocales ());
131- /* -> [
132- { countryCode: "GB", languageTag: "en-GB", languageCode: "en", isRTL: false },
133- { countryCode: "US", languageTag: "en-US", languageCode: "en", isRTL: false },
134- { countryCode: "FR", languageTag: "fr-FR", languageCode: "fr", isRTL: false },
135- ] */
138+ console .log (getCalendar ());
139+ // -> "gregorian"
136140```
137141
138142---
139143
140- ### getNumberFormatSettings ()
144+ ### getCountry ()
141145
142- Returns number formatting settings .
146+ Returns the user current country code (based on its device locale, ** not ** on its position) .
143147
144148#### Method type
145149
146150``` ts
147- type getNumberFormatSettings = () => {
148- decimalSeparator: string ;
149- groupingSeparator: string ;
150- };
151+ type getCountry = () => string ;
151152```
152153
153154#### Usage example
154155
155156``` ts
156- import { getNumberFormatSettings } from " react-native-localize" ;
157+ import { getCountry } from " react-native-localize" ;
157158
158- console .log (getNumberFormatSettings ());
159- /* -> {
160- decimalSeparator: ".",
161- groupingSeparator: ",",
162- } */
159+ console .log (getCountry ());
160+ // -> "FR"
163161```
164162
163+ #### Note
164+
165+ Devices using Latin American regional settings will return "UN" instead of "419", as the latter is not a standard country code.
166+
165167---
166168
167169### getCurrencies()
@@ -185,62 +187,60 @@ console.log(getCurrencies());
185187
186188---
187189
188- ### getCountry ()
190+ ### getLocales ()
189191
190- Returns the user current country code (based on its device locale, ** not ** on its position) .
192+ Returns the user preferred locales, in order .
191193
192194#### Method type
193195
194196``` ts
195- type getCountry = () => string ;
197+ type getLocales = () => Array <{
198+ languageCode: string ;
199+ scriptCode? : string ;
200+ countryCode: string ;
201+ languageTag: string ;
202+ isRTL: boolean ;
203+ }>;
196204```
197205
198206#### Usage example
199207
200208``` ts
201- import { getCountry } from " react-native-localize" ;
209+ import { getLocales } from " react-native-localize" ;
202210
203- console .log (getCountry ());
204- // -> "FR"
211+ console .log (getLocales ());
212+ /* -> [
213+ { countryCode: "GB", languageTag: "en-GB", languageCode: "en", isRTL: false },
214+ { countryCode: "US", languageTag: "en-US", languageCode: "en", isRTL: false },
215+ { countryCode: "FR", languageTag: "fr-FR", languageCode: "fr", isRTL: false },
216+ ] */
205217```
206218
207- #### Note
208-
209- Devices using Latin American regional settings will return "UN" instead of "419", as the latter is not a standard country code.
210-
211219---
212220
213- ### getCalendar ()
221+ ### getNumberFormatSettings ()
214222
215- Returns the user preferred calendar format .
223+ Returns number formatting settings .
216224
217225#### Method type
218226
219227``` ts
220- type getCalendar = () =>
221- | " gregorian"
222- | " buddhist"
223- | " coptic"
224- | " ethiopic"
225- | " ethiopic-amete-alem"
226- | " hebrew"
227- | " indian"
228- | " islamic"
229- | " islamic-umm-al-qura"
230- | " islamic-civil"
231- | " islamic-tabular"
232- | " iso8601"
233- | " japanese"
234- | " persian" ;
228+ type getNumberFormatSettings = () => {
229+ decimalSeparator: string ;
230+ groupingSeparator: string ;
231+ };
235232```
236233
237234#### Usage example
238235
239236``` ts
240- import { getCalendar } from " react-native-localize" ;
237+ import { getNumberFormatSettings } from " react-native-localize" ;
241238
242- console .log (getCalendar ());
243- // -> "gregorian"
239+ console .log (getNumberFormatSettings ());
240+ /* -> {
241+ decimalSeparator: ".",
242+ groupingSeparator: ",",
243+ } */
244244```
245245
246246---
@@ -382,7 +382,7 @@ Returns the best language tag possible and its reading direction. Useful to pick
382382``` ts
383383type findBestLanguageTag = (
384384 languageTags : string [],
385- ) => { languageTag: string ; isRTL: boolean } | void ;
385+ ) => { languageTag: string ; isRTL: boolean } | undefined ;
386386```
387387
388388#### Usage example
@@ -420,6 +420,41 @@ openAppLanguageSettings("application").catch((error) => {
420420});
421421```
422422
423+ ## Server-side rendering
424+
425+ On the client, ` react-native-localize ` uses ` navigator.languages ` . During SSR, it gets language preferences from the server via the parsed ` Accept-Language ` header.
426+
427+ #### 1. Wrap your app with ` ServerLanguagesProvider `
428+
429+ On the server, wrap your app with ` ServerLanguagesProvider ` and pass the user's languages:
430+
431+ ``` tsx
432+ import accepts from " accepts" ;
433+ import { ServerLanguagesProvider } from " react-native-localize" ;
434+
435+ // parse the Accept-Language header; any approach returning string[] is fine
436+ const languages = accepts (request ).languages ();
437+
438+ const html = renderToString (
439+ <ServerLanguagesProvider value = { languages } >
440+ <App />
441+ </ServerLanguagesProvider >,
442+ );
443+ ```
444+
445+ #### 2. Use the ` useLocalize ` hook in your components
446+
447+ In your components, use the ` useLocalize ` hook instead of calling the API methods directly:
448+
449+ ``` tsx
450+ import { useLocalize } from " react-native-localize" ;
451+
452+ const App = () => {
453+ const { getCountry } = useLocalize ();
454+ return <Text >Country: { getCountry ()} </Text >;
455+ };
456+ ```
457+
423458## Examples with [ @formatjs/intl ] ( https://formatjs.io/docs/intl )
424459
425460Browse the files in the [ /example] ( https://github.com/zoontek/react-native-localize/tree/master/example ) directory.
0 commit comments