|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +As mentioned in the [README](README.md), today Replexica works incredibly well only with Nextjs (App Router). |
| 4 | + |
| 5 | +While we're working hard to make it work with other frameworks, let's see how to get started with Nextjs! |
| 6 | + |
| 7 | +> [!TIP] |
| 8 | +> Before applying Replexica to your project, we recommend finishing the tutorial, to learn the basics. |
| 9 | +
|
| 10 | +## Installation |
| 11 | + |
| 12 | +First, let's create a brand new Nextjs project (be sure to choose app router when prompted): |
| 13 | + |
| 14 | +```bash |
| 15 | +pnpm create next-app replexica-demo |
| 16 | +cd replexica-demo |
| 17 | +``` |
| 18 | + |
| 19 | +Then, let's install Replexica: |
| 20 | + |
| 21 | +```bash |
| 22 | +pnpm add replexica @replexica/react @replexica/compiler |
| 23 | +``` |
| 24 | + |
| 25 | +Lastly, let's login to Replexica API: |
| 26 | + |
| 27 | +```bash |
| 28 | +pnpm replexica auth --login |
| 29 | +``` |
| 30 | + |
| 31 | +## Configuration |
| 32 | + |
| 33 | +Now, let's update the content of the `next.config.mjs` file to enable Replexica compiler: |
| 34 | + |
| 35 | +```js |
| 36 | +// next.config.mjs |
| 37 | + |
| 38 | +import replexica from '@replexica/compiler'; |
| 39 | + |
| 40 | +/** @type {import('next').NextConfig} */ |
| 41 | +const nextConfig = {}; |
| 42 | + |
| 43 | +/** @type {import('@replexica/compiler').ReplexicaConfig} */ |
| 44 | +const replexicaConfig = { |
| 45 | + locale: { |
| 46 | + source: 'en', |
| 47 | + targets: ['es'], |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +export default replexica.next( |
| 52 | + replexicaConfig, |
| 53 | + nextConfig, |
| 54 | +); |
| 55 | + |
| 56 | +``` |
| 57 | + |
| 58 | +## Build |
| 59 | + |
| 60 | +If you haven't already, authenticate with the Replexica platform: |
| 61 | + |
| 62 | +```bash |
| 63 | +pnpm replexica auth --login |
| 64 | +``` |
| 65 | + |
| 66 | +Now, let's perform the initial build of our app: |
| 67 | + |
| 68 | +```bash |
| 69 | +pnpm run build |
| 70 | +``` |
| 71 | + |
| 72 | +Next, let's process the translations using the Replexica API: |
| 73 | + |
| 74 | +```bash |
| 75 | +pnpm replexica i18n |
| 76 | +``` |
| 77 | + |
| 78 | +Now, let's build the app again, to inject translations into the build output (Sorry for the double build, we're working on it! 🙏): |
| 79 | + |
| 80 | +```bash |
| 81 | +pnpm run build |
| 82 | +``` |
| 83 | + |
| 84 | +Finally, let's start the app: |
| 85 | + |
| 86 | +```bash |
| 87 | +pnpm run start |
| 88 | +``` |
| 89 | + |
| 90 | +## Test |
| 91 | + |
| 92 | +Now, let's test the app by visiting [http://localhost:3000](http://localhost:3000) in your browser. |
| 93 | + |
| 94 | +You should see the app in English, at first. To quickly check that everything works, run the following code in your browser's console: |
| 95 | + |
| 96 | +```js |
| 97 | +document.cookie = "REPLEXICA_LOCALE=es; path=/;"; |
| 98 | +location.reload(); |
| 99 | +``` |
| 100 | + |
| 101 | +... and you should see the app in Spanish! |
| 102 | + |
| 103 | +To switch back to English, run: |
| 104 | + |
| 105 | +```js |
| 106 | +document.cookie = "REPLEXICA_LOCALE=en; path=/;"; |
| 107 | +location.reload(); |
| 108 | +``` |
| 109 | + |
| 110 | +In the real-world scenario, you would use a language switcher to change the locale. If you need advice on how to implement it fast - send us a message in the [Discord channel](https://discord.gg/GeK6AuSqzw), and we will help! |
| 111 | + |
| 112 | +## Bonus: `use client` components |
| 113 | + |
| 114 | +If you plan on having `'use client'` components *at least somewhere* in your app, you'll need to also wrap your entire component tree in `<ReplexicaProvider />`. |
| 115 | + |
| 116 | +Replace the content of the `src/app/layout.tsx` file with the following: |
| 117 | + |
| 118 | +```tsx |
| 119 | +import { Inter } from "next/font/google"; |
| 120 | +import "./globals.css"; |
| 121 | +import { ReplexicaIntlProvider } from '@replexica/react/client'; |
| 122 | +import { loadLocaleFromCookie } from '@replexica/react/next'; |
| 123 | + |
| 124 | +const inter = Inter({ subsets: ["latin"] }); |
| 125 | + |
| 126 | +export default async function RootLayout({ |
| 127 | + children, |
| 128 | +}: Readonly<{ |
| 129 | + children: React.ReactNode; |
| 130 | +}>) { |
| 131 | + const locale = await loadLocaleFromCookie(); |
| 132 | + // Note the .client.json suffix of the i18n file below. |
| 133 | + // It means that only the values *actually used* get passed to the client, not the entire i18n dictionary. |
| 134 | + const localeData = await import(`@replexica/translations/${locale}.client.json`).then((m) => m.default); |
| 135 | + return ( |
| 136 | + <ReplexicaIntlProvider data={localeData}> |
| 137 | + <html lang={locale}> |
| 138 | + <body className={inter.className}>{children}</body> |
| 139 | + </html> |
| 140 | + </ReplexicaIntlProvider> |
| 141 | + ); |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +To test the `use client` components, let's create a new route. First, create a new file `src/app/client/page.tsx` with the following content: |
| 146 | + |
| 147 | +```tsx |
| 148 | +export { default } from './content'; |
| 149 | +``` |
| 150 | + |
| 151 | +Then, create a new file `src/app/client/content.tsx` with the following content: |
| 152 | + |
| 153 | +```tsx |
| 154 | +'use client'; |
| 155 | + |
| 156 | +export default function Content() { |
| 157 | + return ( |
| 158 | + <div> |
| 159 | + Hello from <a href="https://react.dev/reference/react/use-client"> client component</a>! |
| 160 | + </div> |
| 161 | + ); |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +As you see, there is a `'use client'` directive at the top of the file. As a reminder, that means this particular component will be rendered on the client-side only, and the server will not see it, which is exactly what we want for this example. |
| 166 | + |
| 167 | +Now, let's rebuild the app and fetch missing translations from our LLM-powered Replexica API: |
| 168 | + |
| 169 | +```bash |
| 170 | +pnpm run build |
| 171 | +pnpm replexica i18n |
| 172 | +pnpm run build |
| 173 | +``` |
| 174 | + |
| 175 | +Finally, let's start the app again: |
| 176 | + |
| 177 | +```bash |
| 178 | +pnpm run start |
| 179 | +``` |
| 180 | + |
| 181 | +Now, visit [http://localhost:3000/client](http://localhost:3000/client) in your browser. You should see the `use client` content! |
| 182 | + |
| 183 | +Run the following code in your browser's console to switch the locale to Spanish: |
| 184 | + |
| 185 | +```js |
| 186 | +document.cookie = "REPLEXICA_LOCALE=es; path=/;"; |
| 187 | +location.reload(); |
| 188 | +``` |
| 189 | + |
| 190 | +and the following code to switch back to English: |
| 191 | + |
| 192 | +```js |
| 193 | +document.cookie = "REPLEXICA_LOCALE=en; path=/;"; |
| 194 | +location.reload(); |
| 195 | +``` |
| 196 | + |
| 197 | +## Conclusion |
| 198 | + |
| 199 | +That's it! You've successfully set up Replexica with Nextjs! |
| 200 | + |
| 201 | +Now that you've completed the tutorial, you can start applying Replexica to your own projects. |
| 202 | + |
| 203 | +If you have any questions - drop them in our new [Discord channel](https://discord.gg/GeK6AuSqzw)! |
0 commit comments