### Register All Locales and Initialize svelte-intl-precompile in SvelteKit Layout
Source: https://github.com/cibernox/svelte-intl-precompile/blob/main/README.md
This HTML/JavaScript example demonstrates an alternative approach for initializing `svelte-intl-precompile` in a SvelteKit `__layout.svelte` file. It uses `registerAll()` to automatically register all locale files found in the `$locales` directory. The `init` function is then called with a fallback and the initial locale, similar to the previous example, followed by `waitLocale`.
```html
```
--------------------------------
### Configure Server-Side Rendering (SSR)
Source: https://context7.com/cibernox/svelte-intl-precompile/llms.txt
Setup for SvelteKit applications to extract locale from request headers and initialize the library during the server-side load phase.
```javascript
export function getSession(request) {
const acceptedLanguage = request.headers["accept-language"]?.split(',')[0];
return { acceptedLanguage };
}
```
```svelte
```
--------------------------------
### Initialize Locales in Layout
Source: https://github.com/cibernox/svelte-intl-precompile/blob/main/README.md
Register translation messages and initialize the library within your root layout file. This setup supports both static imports and lazy loading of locale files.
```html
Today: {$date(now)}
Time: {$time(now, { timeStyle: 'medium' })}
Price: {$number(price, { style: 'currency', currency: 'USD' })}
``` -------------------------------- ### Auto-Register All Locales with registerAll in Svelte Source: https://context7.com/cibernox/svelte-intl-precompile/llms.txt Automatically registers all locale files found in the locales directory using `registerAll()` from the `$locales` virtual module. It also provides an `availableLocales` array listing all discovered locales. The `load` function initializes translations and waits for the locale to be ready. ```svelte ``` -------------------------------- ### ICU Translation Configuration and Compilation Source: https://github.com/cibernox/svelte-intl-precompile/blob/main/README.md Demonstrates the transformation of raw ICU message syntax into optimized JavaScript functions using the library's babel plugin. The input JSON defines various translation types, while the output code shows the generated functions that leverage small utility helpers for performance. ```json { "plain": "Some text without interpolations", "interpolated": "A text where I interpolate {count} times", "time": "Now is {now, time}", "number": "My favorite number is {n, number}", "pluralized": "I have {count, plural,=0 {no cats} =1 {one cat} other {{count} cats}}", "pluralized-with-hash": "I have {count, plural, zero {no cats} one {just # cat} other {# cats}}", "selected": "{gender, select, male {He is a good boy} female {She is a good girl} other {They are good fellas}}", "numberSkeleton": "Your account balance is {n, number, ::currency/CAD sign-always}", "installProgress": "{progress, number, ::percent scale/100 .##} completed" } ``` ```javascript import { __interpolate, __number, __plural, __select, __time } from "precompile-intl-runtime"; export default { plain: "Some text without interpolations", interpolated: count => `A text where I interpolate ${__interpolate(count)} times`, time: now => `Now is ${__time(now)}`, number: n => `My favorite number is ${__number(n)}`, pluralized: count => `I have ${__plural(count, { 0: "no cats", 1: "one cat", h: `${__interpolate(count)} cats`})}`, "pluralized-with-hash": count => `I have ${__plural(count, { z: "no cats", o: `just ${count} cat`, h: `${count} cats`})}`, selected: gender => __select(gender, { male: "He is a good boy", female: "She is a good girl", other: "They are good fellas"}), numberSkeleton: n => `Your account balance is ${__number(n, { style: 'currency', currency: 'CAD', signDisplay: 'always' })}`, installProgress: progress => `${__number(progress / 100, { style: 'percent', maximumFractionDigits: 2 })} completed` } ``` -------------------------------- ### Access Translations with $t Store Source: https://context7.com/cibernox/svelte-intl-precompile/llms.txt The $t store is the primary interface for rendering localized strings. It supports simple text, variable interpolation, pluralization, select statements, and number formatting using ICU message syntax. ```svelte{$t('greeting', { name: 'World' })}
{$t('items', { count: 5 })}
{$t('pronoun', { gender: 'female' })}
{$t('price', { amount: 1234.56 })}
``` -------------------------------- ### Translate Content in Svelte Components Source: https://github.com/cibernox/svelte-intl-precompile/blob/main/README.md Use the 't' store provided by the library to translate keys within your Svelte components. The store automatically updates when the locale changes. ```html ``` -------------------------------- ### Define Translation Files Source: https://context7.com/cibernox/svelte-intl-precompile/llms.txt Translations can be defined in various formats including JSON, YAML, and JavaScript. The library automatically transforms ICU message syntax into optimized functions. ```json { "plain": "Some text", "pluralized": "I have {count, plural, one {one cat} other {# cats}}" } ``` ```yaml plain: "Texto sin interpolaciones" pluralized: "Tengo {count, plural, one {un gato} other {{count} gatos}}" ``` ```javascript export default { plain: "Du texte", pluralized: "J'ai {count, plural, one {un chat} other {# chats}}" }; ``` -------------------------------- ### Define Translation JSON Source: https://github.com/cibernox/svelte-intl-precompile/blob/main/README.md Create translation files in JSON format to store key-value pairs for different locales. These files are used by the library to resolve localized strings. ```json { "recent.aria": "Find recently viewed tides", "menu": "Menu", "foot": "{count} {count, plural, =1 {foot} other {feet}}" } ``` -------------------------------- ### Extract Accept-Language Header in SvelteKit Hooks Source: https://github.com/cibernox/svelte-intl-precompile/blob/main/README.md This JavaScript code snippet demonstrates how to extract the 'accept-language' HTTP header from an incoming request within SvelteKit's `getSession` hook. It prioritizes the first language listed in the header and returns it as part of the session object, making it available for SSR locale initialization. ```javascript // src/hooks.js export function getSession(request) { let acceptedLanguage = request.headers["accept-language"] && request.headers["accept-language"].split(',')[0]; return { acceptedLanguage }; } ``` -------------------------------- ### Lazy Load Translations with register in Svelte Source: https://context7.com/cibernox/svelte-intl-precompile/llms.txt Enables lazy loading of translation files using the `register` function in Svelte. This approach loads translations only when needed, which is beneficial for applications with many languages to avoid bundling all translations upfront. The `load` function initializes translations and waits for the locale to be ready. ```svelte ``` -------------------------------- ### Transform ICU Messages with transformCode (JavaScript) Source: https://context7.com/cibernox/svelte-intl-precompile/llms.txt The transformCode function programmatically converts ICU message syntax into optimized JavaScript. This is useful for custom tooling and interactive playgrounds. It takes the ICU message string and an optional configuration object, returning the transformed code. ```javascript import svelteIntlPrecompile from "svelte-intl-precompile/sveltekit-plugin"; const { transformCode } = svelteIntlPrecompile; // Transform ICU messages to optimized JavaScript const input = `export default { "greeting": "Hello, {name}!", "items": "{count, plural, =0 {No items} one {# item} other {# items}}" }`; const output = transformCode(input, { filename: 'en.js' }); // Output: // import { __interpolate, __plural } from "svelte-intl-precompile"; // export default { // "greeting": name => `Hello, ${__interpolate(name)}!`, // "items": count => `${__plural(count, { 0: "No items", o: `${count} item`, h: `${count} items`})}` // }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.