Skip to content

Commit 7ce27f4

Browse files
committed
readme update
1 parent e8fed7b commit 7ce27f4

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

README.md

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ Because all the methods of i18n of JS frontend apps (particularly loading/delive
1212

1313
## What it does
1414

15-
#### Simple example of bundling language packs.
16-
17-
Say you have tree components in your application that you going to bundle with webpack.
15+
Here is nominal case of bundling language packs. Say you have tree components in your application that you going to bundle with webpack.
1816

1917
```
2018
- app/A
@@ -37,7 +35,7 @@ app/C/dict/
3735
ru.json
3836
```
3937

40-
Say due to your app config webpack produces two bundles:
38+
Say you structured the app and configured Webpack to produces two bundles:
4139
- `bundle-AB` - contains `A` and `B` components
4240
- `bundle-C` - separate bundle that contains `C` component.
4341

@@ -51,9 +49,9 @@ bundle-C.en.lp.js - will contain `ru` dictionaries for C
5149
bundle-C.ru.lp.js - will contain `en` dictionaries for C
5250
```
5351

54-
Particular language bundle **will be loaded dynamically on demand** when it will be accessed from it's parent bundle code. If language is not used, language data bundles are not loaded.
52+
Particular language bundle **will be loaded dynamically on demand** when it will be accessed from it's parent bundle code (see below). If language is not used, language data bundles are not loaded.
5553

56-
As stated above, `LP-Loader` can be used to bundle this way not only language specific dictionaries, but **any *labeled* sets of *data* or *code*** that should be loaded dynamically on demand.
54+
As stated above, `LP-Loader` can be used to bundle this way not only language specific dictionaries, but **any *labeled* sets of *data* or *code*** that need to be loaded on demand.
5755

5856
## Installation
5957

@@ -64,17 +62,17 @@ As stated above, `LP-Loader` can be used to bundle this way not only language sp
6462
yarn add lp-loader [--dev]
6563
```
6664

67-
## Configuration
65+
## Usage
6866

69-
This is the example with TypeScript based dictionaries, so you have to have following files:
67+
This is the example with TypeScript based dictionaries (because using TypeScript for language dictionaries data is super cool). So you have to have following files:
7068

7169
```
7270
app/A/
7371
A.ts - component file
7472
app/A/dict/
75-
en.ts - dict for `english` language
76-
ru.ts - dict for `russian` language
77-
index.ts - lp-index file
73+
en.ts - dictionary file for `English` language
74+
ru.ts - dictionary file `Russian` language
75+
index.ts - dictionary index file
7876
7977
```
8078

@@ -83,13 +81,28 @@ You need to have an index file (`app/A/dict/index.ts`) for each of your dictiona
8381
In case of `TS` to have correct and useful dictionary typings code like this should be put inside:
8482

8583
```ts
86-
type Dict = { /* Here goes a dictionary structure */ }
84+
type Dict = {
85+
/* Here goes a dictionary structure */
86+
title: string
87+
}
8788

8889
declare const getDict: (lang: string) => Promise<Dict>
8990

9091
export default getDict
9192
```
9293

94+
Each language dictionary file will look like this:
95+
96+
```ts
97+
import { Dict } from '.'
98+
99+
const dict: Dict = {
100+
title: 'Make i18n Great Again.'
101+
}
102+
103+
export default dict
104+
```
105+
93106
Then you just import this file in your apps code, and use it like this:
94107

95108
```ts
@@ -98,29 +111,29 @@ import getDict from './dict'
98111
let lang = 'en'
99112

100113
// this is how you access dictionary data for particular language
101-
getDict(lang).then(dict => ...)
114+
getDict(lang).then(dict => console.log(dict.name))
102115
```
103116

104-
In `webpack.config` you should define loader for index files:
117+
[*You may want to look at the example app's code.*](./app)
118+
119+
# Configuration
120+
121+
To get this code working in `webpack.config` you should define loader for dictionary index files:
105122

106123
```ts
107124
{
108-
test: /dict(\\|\/)index.ts/, loaders: [
125+
test: /dict(\\|\/)index\.ts/,
126+
loaders: [
109127
{ loader: 'lp-loader', options: { name: 'language.pack' } },
110-
// determine transform loaders for lp-index files:
111-
{ loader: 'ts-loader', options: tsLoaderOptions }
112128
]
113129
},
114130
{
115131
test: /\.ts$/,
116-
loader: 'ts-loader',
117-
// dont forget to exclude lp-index files from general loader rules:
118-
exclude: [/dict(\\|\/)index.ts/],
119-
options: tsLoaderOptions
132+
loader: 'ts-loader'
120133
},
121134
```
122135

123-
[*You may want to look at the example app's code.*](./app)
136+
[*There are example configs in corresponding webpackX folders*](./webpack4/webpack.config.ts). Actually no difference between Webpack 3 and 4.
124137

125138
## Loader options:
126139

@@ -160,11 +173,9 @@ export interface LoaderOptions {
160173
}
161174
```
162175

163-
[*There are example configs in corresponding webpackX folders*](./webpack4/webpack.config.ts). Actually no difference between Webpack 3 and 4.
164-
165176
## How it works
166177

167-
It is quite simple. Loader finds chunk parents of the dictionary index, and generates special code to allow loading requested dictionary data on demand (via `Promise` based API).
178+
It is quite simple. Loader finds chunk parents of the dictionary index, and generates special code that allow loading requested data on demand via `Promise` based API.
168179

169180
[*You may want to look at the loader's code.*](./src/lp-loader.ts)
170181

webpack4/webpack.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ export const makeConfig = (isProduction = false) => {
6161
} as LoaderOptions
6262
} as any,
6363
//{ loader: 'ts-loader', query: tsLoaderOptions } as any
64-
{ loader: path.join(__dirname, 'ts-simple-loader'), query: tsLoaderOptions } as any
64+
//{ loader: path.join(__dirname, 'ts-simple-loader'), query: tsLoaderOptions } as any
6565
]
6666
},
6767
{
6868
test: /\.ts$/,
6969
loader: 'ts-loader',
70-
exclude: [lpTsIndexFiles],
70+
//iexclude: [lpTsIndexFiles],
7171
query: tsLoaderOptions
7272
},
7373
{ test: /\.css$/, loader: 'style!css' },

0 commit comments

Comments
 (0)