Skip to content

Commit 821474c

Browse files
authored
improve configuration information (see #20)
1 parent 373d55e commit 821474c

File tree

1 file changed

+91
-64
lines changed

1 file changed

+91
-64
lines changed

README.md

Lines changed: 91 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -48,67 +48,94 @@ This tells the middleware to use 2 sources in order: `accept-language`, which ha
4848

4949
The name of the lookup used in the priority list always matches the configuration key.
5050

51-
#### priority
52-
Type: `Array` Default value `['accept-language', 'default']`
53-
54-
Defines the order of lookups. The first lookup to return a full locale will be the final result.
55-
56-
Built-in lookups:
57-
* `cookie`
58-
* `query`
59-
* `hostname`
60-
* `accept-language`
61-
* `map`
62-
* `default`
63-
64-
Read below on how to add [custom lookups](#custom-lookups).
65-
66-
#### cookie
67-
Type: `Object` Default value `'{name: 'locale'}'`
68-
69-
The `name` of the cookie that contains the locale for the cookie lookup.
70-
71-
Use with [cookie-parser](https://github.com/expressjs/cookie-parser) middleware.
72-
73-
**Note:** you are responsible for writing the locale to the cookie.
74-
75-
#### query
76-
Type: `Object` Default value `'{name: 'locale'}'`
77-
78-
The `name` of the query string parameter that contains the locale for the query lookup.
79-
80-
#### hostname
81-
Type: `Object` Default value `{}`
82-
83-
A mapping of hostnames to locales for the hostname lookup.
84-
85-
#### map
86-
Type: `Object` Default value `{}`
87-
88-
Maps lookup results that return only a language to a full locale.
89-
90-
#### default
91-
Type: `String` Default value `'en-GB'`
92-
93-
The default locale for the default lookup.
94-
95-
#### allowed
96-
Type: `Array` Default value `undefined`
97-
98-
Lookup results are validated against this list of allowed locales if provided.
99-
100-
#### requestProperty
101-
Type: `String` Default value `'locale'`
102-
103-
By default, the locale is attached to `req.locale` but can be configured with the `requestProperty` option.
104-
105-
## Custom lookups
106-
Add custom lookups or overwrite the default ones by using the `lookups` property:
107-
```javascript
108-
createLocaleMiddleware({
109-
priority: ['custom'],
110-
lookups: {
111-
custom: (req) => req.ip === '127.0.0.1' ? 'en-US' : undefined
112-
}
113-
});
114-
```
51+
### Options
52+
53+
* `priority` Array, default: `['accept-language', 'default']`
54+
55+
Defines the order of lookups. The first lookup to return a full locale will be the final result.
56+
57+
Built-in lookups:
58+
* `cookie`
59+
* `query`
60+
* `hostname`
61+
* `accept-language`
62+
* `map`
63+
* `default`
64+
65+
Read below on how to add custom lookups.
66+
67+
* `allowed` Array
68+
69+
If provided, each lookup's results are validated against this whitelist.
70+
71+
> **Note:** since this validation happens after each lookup, values which will still pass through the next lookup (like when using `map`) need to also be whitelisted as in the example below. This will likely be addressed in a future version (see [#20](https://github.com/smhg/express-locale/issues/20)).
72+
```javascript
73+
// example
74+
createLocaleMiddleware({
75+
priority: ['accept-language', 'cookie', 'map'],
76+
map: {
77+
'en': 'en-US',
78+
'fr': 'fr-CA'
79+
},
80+
allowed: ['en', 'fr', 'en-US', 'fr-CA']
81+
});
82+
```
83+
84+
* `requestProperty` String, default `'locale'`
85+
86+
The property on the request object (`req`) in which the locale is set.
87+
88+
* `cookie` Object, default `'{name: 'locale'}'`
89+
90+
The `name` of the cookie that contains the locale for the cookie lookup.
91+
92+
Use with [cookie-parser](https://github.com/expressjs/cookie-parser) middleware.
93+
94+
* `query` Object, default `'{name: 'locale'}'`
95+
96+
The `name` of the query string parameter that contains the locale for the query lookup.
97+
98+
* `hostname` Object
99+
100+
A mapping of hostnames to locales for the hostname lookup.
101+
```javascript
102+
// example
103+
createLocaleMiddleware({
104+
priority: ['hostname'],
105+
map: {
106+
'en.wikipedia.org': 'en-US',
107+
'nl.wikipedia.org': 'nl-NL'
108+
}
109+
});
110+
```
111+
112+
* `map` Object
113+
114+
Maps lookup results that return only a language to a full locale.
115+
```javascript
116+
// example
117+
createLocaleMiddleware({
118+
priority: ['accept-language', 'cookie', 'map'],
119+
map: {
120+
'en': 'en-US',
121+
'fr': 'fr-CA'
122+
}
123+
});
124+
```
125+
126+
* `default` String, default `'en-GB'`
127+
128+
The default locale for the default lookup.
129+
130+
* `lookups` Object
131+
132+
Add custom lookups or overwrite the default ones by using the `lookups` property.
133+
```javascript
134+
// example
135+
createLocaleMiddleware({
136+
priority: ['custom'],
137+
lookups: {
138+
custom: (req) => req.ip === '127.0.0.1' ? 'en-US' : undefined
139+
}
140+
});
141+
```

0 commit comments

Comments
 (0)