/** * 02 — Locale switching (RU / EN) * * Same site, different language. Backend overlays game names, short * descriptions, and descriptions for the requested locale. Slugs and * IDs never change — safe to share across languages. * * Three ways to pick a locale (highest priority first): * 1. ?locale=en on a per-request basis * 2. constructor option `locale: "en"` → adds Accept-Language header * 3. nothing → API falls back to RU * * Run with: * GAMECORE_API_KEY=gc_live_... bun run examples/02-locale-switching.ts */ import { GameCoreClient } from "@gamecore-api/sdk"; const apiKey = process.env.GAMECORE_API_KEY!; const baseUrl = "https://api.gamecore-api.tech"; // Approach A — one client per locale (simplest) const ru = new GameCoreClient({ apiKey, baseUrl, locale: "ru" }); const en = new GameCoreClient({ apiKey, baseUrl, locale: "en" }); const slug = "shp"; // a game we know has both RU and EN const ruDetail = await ru.catalog.getGame(slug); const enDetail = await en.catalog.getGame(slug); console.log("RU name:", ruDetail.name); console.log("EN name:", enDetail.name); console.log("EN short:", enDetail.shortDescription); // Approach B — single client, switch at runtime (useful for SSR // where the locale comes from the request headers). const gc = new GameCoreClient({ apiKey, baseUrl }); gc.setLocale("en"); console.log("locale now:", gc.getLocale()); const homepage = await gc.catalog.getHomepageGames(); console.log("homepage first:", homepage[0]?.name); // Approach C — per-call override. Useful when most traffic is one // locale but a specific page needs the other (e.g. an admin tool // rendering an EN preview from an otherwise-RU client). const enGame = await gc.catalog.getGame(slug, "en"); console.log("per-call EN:", enGame.name); // Approach D — locale on the paginated list endpoint. Same per-call // override pattern but via a typed option. const { data: enGames } = await gc.catalog.getGames({ limit: 5, locale: "en", }); console.log( "per-call EN list:", enGames.map((g) => g.name), );