Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ msgid "Read the <0>documentation</0> for more info."
msgstr "Přečtěte si <0>dokumentaci</0> pro více informací."
```

Continue with the [React tutorial](https://lingui.dev/tutorials/react), or jump to [React Server Components](https://lingui.dev/tutorials/react-rsc), [React Native](https://lingui.dev/tutorials/react-native), [SolidJS](https://lingui.dev/tutorials/solid), or [plain JavaScript](https://lingui.dev/tutorials/javascript). Working projects for Vite, Next.js, Remix, TanStack Start, React Native and more live in the [examples](https://github.com/lingui/js-lingui/tree/main/examples) directory.
Continue with the [React tutorial](https://lingui.dev/tutorials/react), or jump to [React Server Components](https://lingui.dev/tutorials/react-rsc), [React Native](https://lingui.dev/tutorials/react-native), [SolidJS](https://lingui.dev/tutorials/solid), [Vue](https://lingui.dev/tutorials/vue), or [plain JavaScript](https://lingui.dev/tutorials/javascript). Working projects for Vite, Next.js, Remix, TanStack Start, React Native and more live in the [examples](https://github.com/lingui/js-lingui/tree/main/examples) directory.

## Why Lingui

Expand All @@ -87,7 +87,7 @@ On top of that:

- **Compiled, not parsed at runtime.** Catalogs are compiled ahead of time, so the runtime ships without a MessageFormat parser. Core [![@lingui/core](https://deno.bundlejs.com/?q=%40lingui%2Fcore&treeshake=%5B%7Bi18n%7D%5D&badge=)](https://bundlejs.com/?q=%40lingui%2Fcore), React bindings [![@lingui/react](https://deno.bundlejs.com/?q=%40lingui%2Freact&config=%7B%22esbuild%22%3A%7B%22external%22%3A%5B%22react%22%2C%22%40lingui%2Fcore%22%5D%7D%7D&badge=)](https://bundlejs.com/?q=%40lingui%2Freact&config=%7B%22esbuild%22%3A%7B%22external%22%3A%5B%22react%22%2C%22%40lingui%2Fcore%22%5D%7D%7D).

- **One library for the whole stack.** `@lingui/core` works in any JavaScript project. `@lingui/react` adds components and hooks, including React Server Components support, and `@lingui/solid` brings native SolidJS bindings. React Native uses the same extract-and-compile workflow, Vue single-file components are supported through `@lingui/extractor-vue`, and Astro and Svelte work through community packages.
- **One library for the whole stack.** `@lingui/core` works in any JavaScript project. `@lingui/react` adds components and hooks, including React Server Components support, `@lingui/solid` brings native SolidJS bindings, and `@lingui/vue` brings Vue 3 components, composables and macros that work inside single-file component templates. React Native uses the same extract-and-compile workflow, and Astro and Svelte work through community packages.

- **Standard formats and real tooling.** Translations live in PO files by default, which almost every translation tool understands, or in JSON, CSV, or a custom format. Messages carry comments and context for translators and machine translation. The [CLI](https://lingui.dev/ref/cli) extracts, compiles and validates, the [Vite plugin](https://lingui.dev/ref/vite-plugin) compiles catalogs on the fly, the [SWC plugin](https://lingui.dev/ref/swc-plugin) replaces Babel, and the [ESLint plugin](https://lingui.dev/ref/eslint-plugin) catches common mistakes.

Expand Down
33 changes: 33 additions & 0 deletions examples/vite-project-vue/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?


.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
3 changes: 3 additions & 0 deletions examples/vite-project-vue/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"semi": false
}
13 changes: 13 additions & 0 deletions examples/vite-project-vue/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue + Lingui</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions examples/vite-project-vue/lingui.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from "@lingui/vue/config"
import { createVueExtractor } from "@lingui/extractor-vue"
import babel from "@lingui/cli/api/extractors/babel"

export default defineConfig({
locales: ["en", "pl"],
sourceLocale: "en",
catalogs: [
{
path: "src/locales/{locale}",
include: ["src"],
},
],
extractors: [babel, createVueExtractor()],
})
39 changes: 39 additions & 0 deletions examples/vite-project-vue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "vite-project-vue",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"messages:extract": "lingui extract",
"preview": "vite preview",
"test": "vitest run",
"prettier": "prettier --write '**/*.{ts,tsx,js,jsx,vue}'"
},
"dependencies": {
"@lingui/core": "link:../../packages/core",
"@lingui/vue": "link:../../packages/vue",
"vue": "^3.5.42"
},
"devDependencies": {
"@lingui/babel-plugin-lingui-macro": "link:../../packages/babel-plugin-lingui-macro",
"@lingui/cli": "^6.0.0",
"@lingui/conf": "link:../../packages/conf",
"@lingui/extractor-vue": "link:../../packages/extractor-vue",
"@lingui/vite-plugin": "^6.0.0",
"@vitejs/plugin-vue": "^6.0.8",
"@vitejs/plugin-vue-jsx": "^5.1.6",
"@vue/test-utils": "^2.5.0",
"happy-dom": "^20.0.10",
"prettier": "^3.8.1",
"typescript": "^5.9.3",
"vite": "^7.3.1",
"vitest": "^4.0.18"
},
"resolutions": {
"@lingui/babel-plugin-lingui-macro": "link:../../packages/babel-plugin-lingui-macro",
"@lingui/core": "link:../../packages/core",
"@lingui/conf": "link:../../packages/conf"
}
}
1 change: 1 addition & 0 deletions examples/vite-project-vue/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions examples/vite-project-vue/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
125 changes: 125 additions & 0 deletions examples/vite-project-vue/src/App.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { describe, it, expect, beforeEach, vi } from "vitest"
import { flushPromises, mount, type VueWrapper } from "@vue/test-utils"
import { h, nextTick } from "vue"
import { type I18n, setupI18n } from "@lingui/core"
import { I18nProvider } from "@lingui/vue"
import App from "./App.vue"
import { loadCatalog } from "./i18n"

describe("App", () => {
let i18n: I18n
let wrapper: VueWrapper

const find = (testId: string) => wrapper.find(`[data-testid="${testId}"]`)

const switchLocale = async (name: string, locale: string) => {
await wrapper
.findAll("button")
.find((button) => button.text() === name)!
.trigger("click")
// the catalog is loaded with a dynamic import, wait for the activation
await vi.waitFor(() => expect(i18n.locale).toBe(locale))
await flushPromises()
await nextTick()
}

beforeEach(async () => {
i18n = setupI18n()
await loadCatalog("en", i18n)

wrapper = mount({
render: () => h(I18nProvider, { i18n }, { default: () => h(App) }),
})
})

it("Should render default messages in english and handle language switching", async () => {
expect(find("edit-text").text()).toBe(
"Edit src/App.vue and save to test HMR",
)
expect(find("edit-text").element.innerHTML).toBe(
"Edit <code>src/App.vue</code> and save to test HMR",
)

await switchLocale("Polish", "pl")

expect(find("edit-text").text()).toBe(
"Edytuj src/App.vue i zapisz, aby przetestować HMR",
)
})

it("Should render plural message in EN", async () => {
const pluralBtn = find("plural-button")

expect(pluralBtn.text()).toMatchInlineSnapshot(`"0 months"`)

await pluralBtn.trigger("click")
expect(pluralBtn.text()).toMatchInlineSnapshot(`"1 month"`)

await pluralBtn.trigger("click")
expect(pluralBtn.text()).toMatchInlineSnapshot(`"2 months"`)
})

it("Should render plural message in PL", async () => {
await switchLocale("Polish", "pl")

const pluralBtn = find("plural-button")

expect(pluralBtn.text()).toMatchInlineSnapshot(`"0 miesięcy"`)

await pluralBtn.trigger("click")
expect(pluralBtn.text()).toMatchInlineSnapshot(`"1 miesiąc"`)

await pluralBtn.trigger("click")
expect(pluralBtn.text()).toMatchInlineSnapshot(`"2 miesiące"`)

await pluralBtn.trigger("click")
await pluralBtn.trigger("click")
await pluralBtn.trigger("click")
expect(pluralBtn.text()).toMatchInlineSnapshot(`"5 miesięcy"`)
})

it("Should render using `t` in a template attribute", async () => {
const pluralBtn = find("plural-button")

expect(pluralBtn.attributes("title")).toBe(
"Click on this button to test plurals",
)

await switchLocale("Polish", "pl")

expect(pluralBtn.attributes("title")).toBe(
"Kliknij ten przycisk, aby przetestować formy liczby mnogiej",
)
})

it("Should render lazy messages and `t` from the JSX macro", async () => {
const msgExampleEl = find("msg-example")

expect(msgExampleEl.text()).toMatchInlineSnapshot(`"Red Blue Orange"`)

await switchLocale("Polish", "pl")

expect(msgExampleEl.text()).toBe("Czerwony Niebieski Pomarańczowy")
})

it("Should render select message and react to the selected value", async () => {
const message = find("select-message")
const select = find("gender-select")

expect(message.text()).toBe("His inbox")

await select.setValue("female")
expect(message.text()).toBe("Her inbox")

await select.setValue("other")
expect(message.text()).toBe("Their inbox")

await switchLocale("Polish", "pl")

expect(message.text()).toBe("Ich skrzynka")
expect(find("select-example").text()).toContain("Czyja skrzynka?")

await select.setValue("male")
expect(message.text()).toBe("Jego skrzynka")
})
})
46 changes: 46 additions & 0 deletions examples/vite-project-vue/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script setup lang="ts">
import { Trans, useLingui } from "@lingui/vue/macro"
import vueLogo from "./assets/vue.svg"
import linguiLogo from "./assets/lingui-logo.svg"
import { loadCatalog } from "./i18n"
import PluralExample from "./PluralExample.vue"
import SelectExample from "./SelectExample.vue"
import { MsgExample } from "./MsgExample"
import "./App.css"

const { i18n } = useLingui()
</script>

<template>
<div class="App">
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="/vite.svg" class="logo" alt="Vite logo" />
</a>
<a href="https://vuejs.org" target="_blank">
<img :src="vueLogo" class="logo vue" alt="Vue logo" />
</a>
<a href="https://lingui.dev" target="_blank">
<img :src="linguiLogo" class="logo vue" alt="Lingui logo" />
</a>
</div>
<h1>Vite + Vue + Lingui</h1>
<div class="card">
<PluralExample />
<SelectExample />
<MsgExample />
<p data-testid="edit-text">
<Trans>Edit <code>src/App.vue</code> and save to test HMR</Trans>
</p>
<p>
<button style="margin-right: 20px" @click="loadCatalog('pl', i18n)">
Polish
</button>
<button @click="loadCatalog('en', i18n)">English</button>
</p>
</div>
<p class="read-the-docs">
<Trans>Click on the Vite and Vue logos to learn more</Trans>
</p>
</div>
</template>
Loading
Loading