-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: plugin for intl * chore: readme * chore: changelog * Update runtime.tsx * Update README.md * feat: support intl in server
- Loading branch information
Showing
28 changed files
with
459 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@ice/app': patch | ||
--- | ||
|
||
feat: support generator api to inject code in server entry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
chrome 55 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineConfig } from '@ice/app'; | ||
import intl from '@ice/plugin-intl'; | ||
|
||
export default defineConfig(() => ({ | ||
plugins: [intl()], | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "@examples/with-intl", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "ice start", | ||
"build": "ice build" | ||
}, | ||
"description": "", | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@ice/app": "workspace:*", | ||
"@ice/plugin-intl": "workspace:*", | ||
"@ice/runtime": "workspace:*", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^18.0.0", | ||
"@types/react-dom": "^18.0.2" | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineAppConfig } from 'ice'; | ||
import type { LocaleConfig } from '@ice/plugin-intl/types'; | ||
|
||
export default defineAppConfig(() => ({})); | ||
|
||
export const locale: LocaleConfig = { | ||
getLocale: () => 'en-US', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Meta, Title, Links, Main, Scripts } from 'ice'; | ||
|
||
function Document() { | ||
return ( | ||
<html> | ||
<head> | ||
<meta charSet="utf-8" /> | ||
<meta name="description" content="ICE Demo" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<Meta /> | ||
<Title /> | ||
<Links /> | ||
</head> | ||
<body> | ||
<Main /> | ||
<Scripts /> | ||
</body> | ||
</html> | ||
); | ||
} | ||
|
||
export default Document; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
font-size: 14px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
new: 'New', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"new": "新建" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { intl } from 'ice'; | ||
|
||
export default function Home() { | ||
return ( | ||
<> | ||
<h1>home</h1> | ||
<button>{intl.formatMessage({ id: 'new' })}</button> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="@ice/app/types" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"compileOnSave": false, | ||
"buildOnSave": false, | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"outDir": "build", | ||
"module": "esnext", | ||
"target": "es6", | ||
"jsx": "react-jsx", | ||
"moduleResolution": "node", | ||
"allowSyntheticDefaultImports": true, | ||
"lib": ["es6", "dom"], | ||
"sourceMap": true, | ||
"allowJs": true, | ||
"rootDir": "./", | ||
"forceConsistentCasingInFileNames": true, | ||
"noImplicitReturns": true, | ||
"noImplicitThis": true, | ||
"noImplicitAny": false, | ||
"importHelpers": true, | ||
"strictNullChecks": true, | ||
"suppressImplicitAnyIndexErrors": true, | ||
"noUnusedLocals": true, | ||
"skipLibCheck": true, | ||
"paths": { | ||
"@/*": ["./src/*"], | ||
"ice": [".ice"] | ||
} | ||
}, | ||
"include": ["src", ".ice", "ice.config.*"], | ||
"exclude": ["build", "public"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @ice/plugin-intl | ||
|
||
## 1.0.0 | ||
|
||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# @ice/plugin-intl | ||
|
||
`@ice/plugin-intl` is a ice.js plugin. It provides a simple way to add internationalization support to your application. | ||
|
||
> `@ice/plugin-intl` is based on `react-intl`. | ||
## Install | ||
|
||
```bash | ||
$ npm i @ice/plugin-intl --save-dev | ||
``` | ||
|
||
## Usage | ||
|
||
Define the plugin in `ice.config.mts`: | ||
|
||
```ts | ||
import { defineConfig } from '@ice/app'; | ||
import intl from '@ice/plugin-intl'; | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
intl(), | ||
], | ||
}); | ||
``` | ||
|
||
Define locale config in `src/app.ts`: | ||
|
||
```ts | ||
import { defineAppConfig } from 'ice'; | ||
import type { LocaleConfig } from '@ice/plugin-intl/types'; | ||
|
||
export default defineAppConfig(() => ({})); | ||
|
||
export const locale: LocaleConfig = { | ||
// Cutomize getLocale method and other options supported by react-intl. | ||
getLocale: () => 'en-US', | ||
}; | ||
``` | ||
|
||
## Locales | ||
|
||
Locales are defined in the `src/locales` directory. Each locale is defined in a separate file, with the locale name as the file name. For example, `en-US.ts`: | ||
|
||
```ts | ||
export default { | ||
'app.title': 'My Application', | ||
'app.welcome': 'Welcome to my application!', | ||
}; | ||
``` | ||
|
||
Use the `useIntl` hook to access the current locale: | ||
|
||
```tsx | ||
import { useIntl } from 'ice'; | ||
|
||
export default function Home() { | ||
const intl = useIntl(); | ||
console.log(intl.formatMessage({ id: 'new' })); | ||
return <h1>home</h1>; | ||
} | ||
``` | ||
|
||
Use the `intl` function to access the current locale: | ||
|
||
```tsx | ||
import { intl } from 'ice'; | ||
|
||
function alertMessage() { | ||
alert(intl.formatMessage({ id: 'app.welcome' })); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "@ice/plugin-intl", | ||
"version": "1.0.0", | ||
"description": "react intl plugin for ice.js 3.", | ||
"files": [ | ||
"esm", | ||
"!esm/**/*.map", | ||
"*.d.ts", | ||
"templates" | ||
], | ||
"type": "module", | ||
"main": "esm/index.js", | ||
"module": "esm/index.js", | ||
"types": "esm/index.d.ts", | ||
"exports": { | ||
".": "./esm/index.js", | ||
"./runtime": "./esm/runtime.js", | ||
"./types": "./esm/types.js" | ||
}, | ||
"sideEffects": false, | ||
"scripts": { | ||
"watch": "tsc -w --sourceMap", | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"react-intl": "^6.0.0", | ||
"fast-glob": "^3.3.2" | ||
}, | ||
"devDependencies": { | ||
"@ice/app": "^3.3.2", | ||
"@ice/runtime": "^1.2.9", | ||
"@types/react": "^18.0.0", | ||
"@types/react-dom": "^18.0.0" | ||
}, | ||
"repository": { | ||
"type": "http", | ||
"url": "https://github.com/alibaba/ice/tree/master/packages/plugin-intl" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './esm/runtime'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import fg from 'fast-glob'; | ||
import type { Plugin } from '@ice/app/types'; | ||
|
||
const _dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
const plugin: Plugin = () => ({ | ||
name: 'plugin-intl', | ||
setup: ({ generator, context, createLogger, watch }) => { | ||
const { rootDir } = context; | ||
const logger = createLogger('plugin-intl'); | ||
|
||
const renderLocaleEntry = (localeFiles: string[]) => { | ||
const locales = []; | ||
let localeExport = []; | ||
localeFiles.forEach((file) => { | ||
const filename = path.basename(file, path.extname(file)); | ||
// `-` is not allowed in import specifier. | ||
const specifier = filename.replace('-', '_'); | ||
locales.push(`import ${specifier} from '@/locales/${filename}';`); | ||
localeExport.push(`'${filename}': ${specifier},`); | ||
}); | ||
|
||
generator.addRenderFile( | ||
path.join(_dirname, '../templates/locales.ts.ejs'), | ||
'locales.ts', | ||
{ | ||
localeImport: locales.join('\n'), | ||
localeExport: localeExport.join('\n '), | ||
}, | ||
); | ||
}; | ||
const globRule = 'src/locales/*.{ts,js,json}'; | ||
// Glob all locale files, and generate runtime options. | ||
const localeFiles = fg.sync(globRule, { cwd: rootDir }); | ||
if (localeFiles.length > 0) { | ||
// Filter the entry of locale files. | ||
const mainEntry = localeFiles.find((file) => file.match(/index\.(ts|js|json)$/)); | ||
let runtimeSource = ''; | ||
if (mainEntry) { | ||
runtimeSource = `@/locales/${path.basename(mainEntry)}`; | ||
} else { | ||
// Create a locale entry file to export all locale files. | ||
renderLocaleEntry(localeFiles); | ||
|
||
// Add watch event for locale files added or removed. | ||
watch.addEvent([/src\/locales/, (event) => { | ||
if (event === 'unlink' || event === 'add') { | ||
const files = fg.sync(globRule, { cwd: rootDir }); | ||
renderLocaleEntry(files); | ||
} | ||
}]); | ||
runtimeSource = './locales'; | ||
|
||
generator.addEntryImportAhead({ | ||
source: runtimeSource, | ||
}, 'both'); | ||
} | ||
} else { | ||
logger.warn('No locale files found, please check the `src/locales` folder.'); | ||
} | ||
|
||
// Add intl export from ice. | ||
generator.addExport({ | ||
specifier: ['useIntl', 'intl'], | ||
source: '@ice/plugin-intl/runtime', | ||
}); | ||
}, | ||
runtime: '@ice/plugin-intl/runtime', | ||
}); | ||
|
||
export default plugin; |
Oops, something went wrong.