Skip to content

Commit

Permalink
add translation system
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnedc committed Aug 27, 2023
1 parent 9e3da7b commit 42ff0e1
Show file tree
Hide file tree
Showing 11 changed files with 3,590 additions and 3,258 deletions.
6,646 changes: 3,434 additions & 3,212 deletions package-lock.json

Large diffs are not rendered by default.

74 changes: 38 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
{
"name": "okey-masasi-sveltekit",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write ."
},
"devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/adapter-static": "^2.0.3",
"@sveltejs/kit": "^1.20.4",
"@types/uuid": "^9.0.2",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-svelte": "^2.30.0",
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.10.1",
"svelte": "^4.0.5",
"svelte-check": "^3.4.3",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.4.2"
},
"type": "module",
"dependencies": {
"@fortawesome/fontawesome-free": "^6.4.2",
"bulma": "^0.9.4",
"uuid": "^9.0.0"
}
"name": "okey-masasi-sveltekit",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write .",
"extract-messages": "svelte-i18n extract \"./src/**/*.svelte\" -s > ./src/lib/locales/en.json"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/adapter-static": "^2.0.3",
"@sveltejs/kit": "^1.20.4",
"@types/uuid": "^9.0.2",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-svelte": "^2.30.0",
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.10.1",
"svelte": "^4.0.5",
"svelte-check": "^3.4.3",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.4.2"
},
"type": "module",
"dependencies": {
"@fortawesome/fontawesome-free": "^6.4.2",
"bulma": "^0.9.4",
"svelte-i18n": "^3.7.0",
"uuid": "^9.0.0"
}
}
22 changes: 22 additions & 0 deletions src/lib/app.messages.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script context="module">
import { defineMessages } from 'svelte-i18n'
export const messages = defineMessages({
appTitle: {
id: 'app.appTitle',
default: 'Okey table',
},
newGame: {
id: 'app.newGame',
default: 'New game',
},
save: {
id: 'app.save',
default: 'Save',
},
cancel: {
id: 'app.cancel',
default: 'Cancel',
},
})
</script>
22 changes: 22 additions & 0 deletions src/lib/components/GameForm.messages.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script context="module">
import { defineMessages } from 'svelte-i18n'
export const messages = defineMessages({
playerTitle: {
id: 'gameForm.playerTitle',
default: 'Player {number}',
},
playerNamePlaceHolder: {
id: 'gameForm.playerNamePlaceHolder',
default: "Player {number}'s name",
},
colourPointsLabel: {
id: 'gameForm.colourPointsLabel',
default: 'Colour points',
},
indicatorPointLabel: {
id: 'gameForm.indicatorPointLabel',
default: 'Indicator points',
},
})
</script>
28 changes: 20 additions & 8 deletions src/lib/components/GameForm.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<script lang="ts">
import { colours, colourToBulmaMap, type CreateNewGameProps, type Game } from '$lib/types'
import { _ } from 'svelte-i18n'
import { messages } from './GameForm.messages.svelte'
import { messages as appMessages } from '$lib/app.messages.svelte'
export let game: Game | undefined = undefined
export let onCancelClick: () => void
Expand Down Expand Up @@ -28,13 +31,19 @@
<div class="content">
{#each [0, 1, 2, 3] as playerIndex}
<div class="field">
<label for={`player${playerIndex}`} class="label">Player {playerIndex + 1}</label>
<label for={`player${playerIndex}`} class="label">
{$_(messages.playerTitle.id, {
values: { number: playerIndex + 1 },
})}
</label>
<div class="control has-icons-left">
<input
id={`player${playerIndex}`}
class="input"
type="text"
placeholder={`Player ${playerIndex + 1}'s name`}
placeholder={$_(messages.playerNamePlaceHolder.id, {
values: { number: playerIndex + 1 },
})}
bind:value={playerNames[playerIndex]}
required
/>
Expand All @@ -46,7 +55,7 @@
{/each}
</div>

<div class="label">Colour points</div>
<div class="label">{$_(messages.colourPointsLabel.id)}</div>
<div class="columns is-mobile is-multiline">
{#each colours as colour}
<div class="column is-half">
Expand All @@ -56,7 +65,6 @@
id={`colour-${colour}`}
class="input"
type="number"
placeholder={`${colour + 1}'s points`}
bind:value={colourPoints[colour]}
min="1"
required
Expand All @@ -71,9 +79,9 @@
</div>

<div class="field">
<label for={`gostergePoint`} class="label">Gosterge point</label>
<label for="indicatorPoint" class="label">{$_(messages.indicatorPointLabel.id)}</label>
<div class="control has-icons-left">
<input id="gostergePoint" class="input" type="number" bind:value={gostergePoint} required />
<input id="indicatorPoint" class="input" type="number" bind:value={gostergePoint} required />
<span class="icon is-small is-left">
<i class="fas fa-user" />
</span>
Expand All @@ -83,12 +91,16 @@
<div class="level is-mobile">
<div class="level-right">
<div class="level-item">
<button class="button is-danger" type="button" on:click={onCancelClick}>Cancel</button>
<button class="button is-danger" type="button" on:click={onCancelClick}>
{$_(appMessages.cancel.id)}
</button>
</div>
</div>
<div class="level-left">
<div class="level-item">
<button class="button is-primary" type="submit">Save</button>
<button class="button is-primary" type="submit">
{$_(appMessages.save.id)}
</button>
</div>
</div>
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/lib/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { init, getLocaleFromNavigator, register } from 'svelte-i18n'

const supportedLanguages = ['en', 'tr', 'de'] as const

supportedLanguages.forEach((locale) => {
register(locale, () => import(`./locales/${locale}.json`))
})

init({
fallbackLocale: 'tr',
initialLocale: getLocaleFromNavigator(),
})
10 changes: 10 additions & 0 deletions src/lib/locales/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"app.appTitle": "Okey-Tisch",
"app.newGame": "Neues Spiel",
"app.save": "Speichern",
"app.cancel": "Abbrechen",
"gameForm.playerTitle": "Spieler {number}",
"gameForm.playerNamePlaceHolder": "Der Name von Spieler {number}",
"gameForm.colourPointsLabel": "Farbpunkte",
"gameForm.indicatorPointLabel": "Indikatorpunkte"
}
10 changes: 10 additions & 0 deletions src/lib/locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"app.appTitle": "Okey table",
"app.newGame": "New game",
"app.save": "Save",
"app.cancel": "Cancel",
"gameForm.playerTitle": "Player {number}",
"gameForm.playerNamePlaceHolder": "Player {number}'s name",
"gameForm.colourPointsLabel": "Colour points",
"gameForm.indicatorPointLabel": "Indicator points"
}
10 changes: 10 additions & 0 deletions src/lib/locales/tr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"app.appTitle": "Okey masası",
"app.newGame": "Yeni oyun",
"app.save": "Kaydet",
"app.cancel": "Vazgeç",
"gameForm.playerTitle": "{number}. oyuncu",
"gameForm.playerNamePlaceHolder": "{number}. oyuncu ismi",
"gameForm.colourPointsLabel": "Renk puanları",
"gameForm.indicatorPointLabel": "Gösterge puanı"
}
8 changes: 8 additions & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<script>
import 'bulma/css/bulma.css'
import '@fortawesome/fontawesome-free/css/all.css'
import '$lib/i18n'
import { waitLocale } from 'svelte-i18n'
export async function preload() {
// awaits for the loading of the 'en-US' and 'en' dictionaries
return waitLocale()
}
</script>

<slot />
6 changes: 4 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import Modal from '$lib/components/Modal.svelte'
import { games } from '$lib/stores/games'
import type { CreateNewGameProps, Game } from '$lib/types'
import { _, defineMessages } from 'svelte-i18n'
import { messages } from '$lib/app.messages.svelte'
let isNewGameModalVisible = false
let isEditGameModalVisible = false
Expand Down Expand Up @@ -33,13 +35,13 @@
</script>

<main class="container">
<h1 class="title">Okey Masasi</h1>
<h1 class="title">{$_(messages.appTitle)}</h1>

<button type="button" class="button is-info" on:click={() => (isNewGameModalVisible = true)}>
<span class="icon">
<i class="fab fa-plus" />
</span>
<span>New game</span>
<span>{$_(messages.newGame)}</span>
</button>

<div class="block" />
Expand Down

0 comments on commit 42ff0e1

Please sign in to comment.