-
Notifications
You must be signed in to change notification settings - Fork 407
feat: add full support of Vue.js #1925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
JSteunou
wants to merge
25
commits into
lingui:next
Choose a base branch
from
JSteunou:add_new_lingui_vue
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
078e564
feat: add @lingui/vue
JSteunouSteeple d382f31
chore: add publishConfig for CI
JSteunou a3d42ca
Merge branch 'refs/heads/next' into add_new_lingui_vue
timofei-iatsenko ab300de
refactor js macro
timofei-iatsenko 41dc5f6
use lingui core internals for vue vt and trans macro
timofei-iatsenko a67c1ed
support vt transformation in interpolation
timofei-iatsenko a2a03da
WIP: Plural Element support
timofei-iatsenko aba8de6
remove Plural Element, implement support for inline ICU expression
timofei-iatsenko a21222a
a bit better code lens style in the function comments
timofei-iatsenko 8a58e5f
fix: remove some comments & fix some TS errors
JSteunou 1a6284a
feat(react): fix macro export conditions for NodeNext and Bundler res…
timofei-iatsenko 22f22e1
fix(experimental-extractor): fix various bugs (#1958)
timofei-iatsenko 5a6abff
Merge branch 'next' into add_new_lingui_vue
JSteunou 3a0d8bd
fix: typescript expected error on predicate call
JSteunou 68c190e
fix: macroJst.test needed to follow refacto from previous commit
JSteunou 1d98e19
fix ts errors and tests
timofei-iatsenko b87b64e
prettier
timofei-iatsenko 8ddc0b1
remove runtime vt function
timofei-iatsenko effd2c0
add vue-vite-plugin
timofei-iatsenko 9a49f8f
fix: remove default export
JSteunou f0b1689
fix: transformer signature has changed
JSteunou bf54052
chore: go from 4.8.0-next.1 to 5.0.0-next.0
JSteunou 10810dd
Merge branch 'next' into add_new_lingui_vue
timofei-iatsenko 0b55212
fix issue after merge with `next`
timofei-iatsenko bd50953
Merge branch 'next' into add_new_lingui_vue
timofei-iatsenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
[![License][badge-license]][license] | ||
[![Version][badge-version]][package] | ||
[![Downloads][badge-downloads]][package] | ||
|
||
# @lingui/vue | ||
|
||
> vue components for internationalization | ||
|
||
`@lingui/vue` is part of [LinguiJS][linguijs]. See the [documentation][documentation] for all information, tutorials and examples. | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install --save @lingui/vue | ||
# yarn add @lingui/vue | ||
``` | ||
|
||
## Usage | ||
|
||
See the [tutorial][tutorial] or [reference][reference] documentation. | ||
|
||
## License | ||
|
||
[MIT][license] | ||
|
||
[license]: https://github.com/lingui/js-lingui/blob/main/LICENSE | ||
[linguijs]: https://github.com/lingui/js-lingui | ||
[documentation]: https://lingui.dev | ||
[tutorial]: https://lingui.dev/tutorials/vue | ||
[reference]: https://lingui.dev/ref/vue | ||
[package]: https://www.npmjs.com/package/@lingui/vue | ||
[badge-downloads]: https://img.shields.io/npm/dw/@lingui/vue.svg | ||
[badge-version]: https://img.shields.io/npm/v/@lingui/vue.svg | ||
[badge-license]: https://img.shields.io/npm/l/@lingui/vue.svg |
This file contains hidden or 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,116 @@ | ||
{ | ||
"name": "@lingui/vue", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"version": "5.0.0-next.0", | ||
"sideEffects": false, | ||
"description": "Vue components & tools for translations", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"author": { | ||
"name": "Jérôme Steunou", | ||
"email": "jerome.steunou@gmail.com" | ||
}, | ||
"license": "MIT", | ||
"keywords": [ | ||
"vue", | ||
"component", | ||
"i18n", | ||
"internationalization", | ||
"i9n", | ||
"translation", | ||
"icu", | ||
"messageformat", | ||
"multilingual", | ||
"localization", | ||
"l10n" | ||
], | ||
"scripts": { | ||
"build": "rimraf ./dist && unbuild", | ||
"stub": "unbuild --stub" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lingui/js-lingui.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/lingui/js-lingui/issues" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0" | ||
}, | ||
"exports": { | ||
".": { | ||
"require": { | ||
"types": "./dist/index.d.cts", | ||
"default": "./dist/index.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/index.d.mts", | ||
"default": "./dist/index.mjs" | ||
} | ||
}, | ||
"./compiler": { | ||
"require": { | ||
"types": "./dist/compiler/index.d.cts", | ||
"default": "./dist/compiler/index.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/compiler/index.d.mts", | ||
"default": "./dist/compiler/index.mjs" | ||
} | ||
}, | ||
"./extractor": { | ||
"require": { | ||
"types": "./dist/extractor/index.d.cts", | ||
"default": "./dist/extractor/index.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/extractor/index.d.mts", | ||
"default": "./dist/extractor/index.mjs" | ||
} | ||
}, | ||
"./vite-plugin": { | ||
"require": { | ||
"types": "./dist/vite-plugin/index.d.cts", | ||
"default": "./dist/vite-plugin/index.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/vite-plugin/index.d.mts", | ||
"default": "./dist/vite-plugin/index.mjs" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"dist/" | ||
], | ||
"dependencies": { | ||
"@babel/core": "^7.20.12", | ||
"@babel/generator": "^7.20.12", | ||
"@babel/types": "^7.20.12", | ||
"@lingui/babel-plugin-lingui-macro": "5.0.0-next.0", | ||
"@lingui/cli": "5.0.0-next.0", | ||
"@lingui/core": "5.0.0-next.0", | ||
"@lingui/message-utils": "5.0.0-next.0", | ||
"@lingui/vite-plugin": "5.0.0-next.0", | ||
"@vue/compiler-core": "^3.3.4", | ||
"@vue/compiler-sfc": "^3.3.4", | ||
"vue": "^3.3.4" | ||
}, | ||
"peerDependencies": { | ||
"@vitejs/plugin-vue": "*", | ||
"vite": "^3 || ^4 || ^5.0.9" | ||
}, | ||
"devDependencies": { | ||
"@lingui/conf": "5.0.0-next.0", | ||
"@types/babel__core": "^7.20.5", | ||
"@vitejs/plugin-vue": "^5.0.5", | ||
"unbuild": "2.0.0", | ||
"vite": "4.1.4" | ||
} | ||
} |
This file contains hidden or 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,202 @@ | ||
import { | ||
type ElementNode, | ||
findProp, | ||
TemplateChildNode, | ||
SimpleExpressionNode, | ||
} from "@vue/compiler-core" | ||
|
||
import { | ||
isAttributeNode, | ||
isElementNode, | ||
isInterpolationNode, | ||
isSimpleExpressionNode, | ||
isTextNode, | ||
} from "./predicates" | ||
import { | ||
TextToken, | ||
Token, | ||
JsMacroName, | ||
ElementToken, | ||
ArgToken, | ||
isArgDecorator, | ||
tokenizeArg, | ||
MacroJsContext, | ||
} from "@lingui/babel-plugin-lingui-macro/ast" | ||
import * as t from "@babel/types" | ||
import { | ||
tokenizeAsChoiceComponentOrUndefined, | ||
tokenizeAsLinguiTemplateLiteralOrUndefined, | ||
} from "../compiler/transformVt" | ||
|
||
export const makeCounter = | ||
(index = 0) => | ||
() => | ||
index++ | ||
|
||
export type MacroTransVueContext = { | ||
// Positional expressions counter (e.g. for placeholders `Hello {0}, today is {1}`) | ||
getExpressionIndex: () => number | ||
getElementIndex: () => number | ||
stripNonEssentialProps: boolean | ||
isLinguiIdentifier: (node: t.Identifier, macro: JsMacroName) => boolean | ||
} | ||
|
||
export function createMacroTransVueContext( | ||
isLinguiIdentifier: MacroTransVueContext["isLinguiIdentifier"], | ||
stripNonEssentialProps: boolean | ||
): MacroTransVueContext { | ||
return { | ||
getExpressionIndex: makeCounter(), | ||
getElementIndex: makeCounter(), | ||
isLinguiIdentifier, | ||
stripNonEssentialProps, | ||
} | ||
} | ||
|
||
function tokenizeText(value: string): TextToken { | ||
return { | ||
type: "text", | ||
value, | ||
} | ||
} | ||
|
||
/** | ||
* Receive a Vue element which is none of the Lingui macro, and returns Tokens for | ||
* element itself and all of it's children | ||
* | ||
* ``` | ||
* <Trans>Hello <strong>world!</strong></Trans> | ||
* ^^^^^^^^^^^^^^^^^^^^^^^ | ||
* ``` | ||
*/ | ||
function tokenizeElementNode( | ||
node: ElementNode, | ||
ctx: MacroTransVueContext | ||
): ElementToken { | ||
// !!! Important: Calculate element index before traversing children. | ||
// That way outside elements are numbered before inner elements. (...and it looks pretty). | ||
const name = ctx.getElementIndex() | ||
|
||
return { | ||
type: "element", | ||
name, | ||
value: node, | ||
children: node.children | ||
.flatMap((child) => tokenizeChildren(child, ctx)) | ||
.filter(Boolean) as Token[], | ||
} | ||
} | ||
|
||
/** | ||
* Take `<Trans>Hello <strong>{{name}}</strong></Trans>` and returns Tokens | ||
*/ | ||
export function tokenizeTrans(node: ElementNode, ctx: MacroTransVueContext) { | ||
return node.children | ||
.flatMap((child) => tokenizeChildren(child, ctx)) | ||
.filter(Boolean) as Token[] | ||
} | ||
|
||
/** | ||
* Process children of Elements and returns Tokens | ||
*/ | ||
function tokenizeChildren( | ||
node: TemplateChildNode, | ||
ctx: MacroTransVueContext | ||
): Token[] { | ||
// <Trans>Hello <strong>Username</strong></Trans> | ||
// ^^^^^^ | ||
if (isTextNode(node)) { | ||
return [tokenizeText(node.content)] | ||
} | ||
|
||
// <Trans>Hello <strong>Username</strong></Trans> | ||
// ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
// goes into recursion for inner Element Nodes | ||
if (isElementNode(node)) { | ||
return [tokenizeElementNode(node, ctx)] | ||
} | ||
|
||
// <Trans>Hello {{ username }}</Trans> | ||
// ^^^^^^^^^^^^^^ | ||
if (isInterpolationNode(node) && isSimpleExpressionNode(node.content)) { | ||
return tokenizeSimpleExpressionNode(node.content, ctx) | ||
} | ||
|
||
return [] | ||
} | ||
|
||
/** | ||
* Take an expression from Vue interpolation and returns Tokens | ||
* | ||
* ``` | ||
* <Trans>Hello {{ username }}</Trans> | ||
* ^^^^^^^^^^^^^ | ||
* ``` | ||
* | ||
* Supported expressions: | ||
* | ||
* - {{ username }} -> {username} | ||
* - {{ user.name }} -> {0} | ||
* - {{ plural(...) }} | {{ select(...) }} -> {count, plural, one {...} other {...}} | ||
* - {{ arg(username) }} -> username | ||
*/ | ||
function tokenizeSimpleExpressionNode( | ||
node: SimpleExpressionNode, | ||
ctx: MacroTransVueContext | ||
): Token[] { | ||
if (node.ast) { | ||
const vtTokens = tokenizeVt(node.ast, ctx) | ||
|
||
if (vtTokens) { | ||
return vtTokens | ||
} | ||
|
||
if (t.isCallExpression(node.ast) && isArgDecorator(node.ast, ctx)) { | ||
return [tokenizeArg(node.ast, ctx)] | ||
} | ||
|
||
return [ | ||
{ | ||
type: "arg", | ||
name: String(ctx.getExpressionIndex()), | ||
value: node.ast as t.Expression, | ||
} satisfies ArgToken, | ||
] | ||
} | ||
|
||
// For simple interpolation with identifier | ||
// only Vue doesn't populate an `.ast` | ||
return [ | ||
{ | ||
type: "arg", | ||
name: node.content, | ||
value: t.identifier(node.content), | ||
} satisfies ArgToken, | ||
] | ||
} | ||
|
||
function tokenizeVt(node: t.Node, ctx: MacroJsContext) { | ||
for (const fn of [ | ||
tokenizeAsChoiceComponentOrUndefined, | ||
tokenizeAsLinguiTemplateLiteralOrUndefined, | ||
]) { | ||
const tokens = fn(node, ctx) | ||
|
||
if (tokens) { | ||
return tokens | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
export function getTextProp( | ||
node: ElementNode, | ||
propName: string | ||
): string | undefined { | ||
const prop = findProp(node, propName, undefined, false) | ||
if (isAttributeNode(prop) && prop.value?.content) { | ||
return prop.value.content | ||
} | ||
return undefined | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.