Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 3cbbf9c

Browse files
author
Je
committed
refactor: add locales field for config
1 parent 63f0fb0 commit 3cbbf9c

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

project.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Routing } from './router.ts'
99
import { colors, ensureDir, path, ServerRequest, Sha1, walk } from './std.ts'
1010
import { compile } from './tsc/compile.ts'
1111
import type { AlephRuntime, APIHandle, Config, RouterURL } from './types.ts'
12-
import util, { existsDirSync, existsFileSync, hashShort, reHashJs, reHttp, reMDExt, reModuleExt, reStyleModuleExt } from './util.ts'
12+
import util, { existsDirSync, existsFileSync, hashShort, reHashJs, reHttp, reLocaleID, reMDExt, reModuleExt, reStyleModuleExt } from './util.ts'
1313
import { cleanCSS, Document, less } from './vendor/mod.ts'
1414
import { version } from './version.ts'
1515

@@ -54,6 +54,7 @@ export class Project {
5454
outputDir: '/dist',
5555
baseUrl: '/',
5656
defaultLocale: 'en',
57+
locales: [],
5758
ssr: {
5859
fallback: '404.html'
5960
},
@@ -206,7 +207,7 @@ export class Project {
206207
{ src: path.join(baseUrl, `/_aleph/main.${mainModule.hash.slice(0, hashShort)}.js`), type: 'module' },
207208
{ src: path.join(baseUrl, `/_aleph/-/deno.land/x/aleph/nomodule.js${this.isDev ? '?dev' : ''}`), nomodule: true },
208209
],
209-
body: `<main></main>`,
210+
body: `<main><p><em>Loading...</em></p></main>`, // todo: custom `loading` page
210211
minify: !this.isDev
211212
})
212213
return html
@@ -346,6 +347,7 @@ export class Project {
346347
buildTarget,
347348
sourceMap,
348349
defaultLocale,
350+
locales,
349351
ssr,
350352
env
351353
} = config
@@ -367,6 +369,10 @@ export class Project {
367369
if (util.isNEString(defaultLocale)) {
368370
Object.assign(this.config, { defaultLocale })
369371
}
372+
if (util.isArray(locales)) {
373+
Object.assign(this.config, { locales: Array.from(new Set(locales.filter(l => reLocaleID.test(l)))) })
374+
locales.filter(l => !reLocaleID.test(l)).forEach(l => log.warn(`invalid locale ID '${l}'`))
375+
}
370376
if (typeof ssr === 'boolean') {
371377
Object.assign(this.config, { ssr })
372378
} else if (util.isPlainObject(ssr)) {
@@ -381,7 +387,7 @@ export class Project {
381387
// Gen build ID after config loaded.
382388
this.#buildID = (new Sha1()).update(this.mode + '.' + this.config.buildTarget + '.' + version).hex().slice(0, 18)
383389
// Update routing options.
384-
this.#routing = new Routing([], this.config.baseUrl, this.config.defaultLocale, [])
390+
this.#routing = new Routing([], this.config.baseUrl, this.config.defaultLocale, this.config.locales)
385391
}
386392

387393
private async _init() {

types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface Config {
1616
readonly outputDir: string
1717
readonly baseUrl: string
1818
readonly defaultLocale: string
19+
readonly locales: string[]
1920
readonly ssr: boolean | SSROptions
2021
readonly buildTarget: string
2122
readonly sourceMap: boolean

util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const reHttp = /^https?:\/\//i
77
export const reModuleExt = /\.(js|jsx|mjs|ts|tsx)$/i
88
export const reStyleModuleExt = /\.(css|less|sass|scss)$/i
99
export const reMDExt = /\.(md|markdown)$/i
10+
export const reLocaleID = /^[a-z]{2}(-[a-zA-Z0-9]+)?$/
1011
export const reHashJs = new RegExp(`\\.[0-9a-fx]{${hashShort}}\\.js$`, 'i')
1112

1213
export default {

0 commit comments

Comments
 (0)