Skip to content

Commit

Permalink
chroe(guide): detect language based on hostname (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung authored Apr 1, 2024
1 parent 1c6e7e5 commit 09142b1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
15 changes: 10 additions & 5 deletions guide/app/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ import type { AppLoadContext } from '@remix-run/cloudflare';
interface Language {
code: string;
label: string;
branch: string;
docPath: string;
domain: string;
isDecodeUtf8: boolean;
}

export const allLanguages: Language[] = [
{
code: 'en',
label: 'en',
branch: 'main',
docPath: 'docs',
domain: 'conform.guide',
isDecodeUtf8: false,
},
{
code: 'ja',
label: 'ja',
branch: 'ja',
docPath: 'docs/ja',
domain: 'ja.conform.guide',
isDecodeUtf8: true,
Expand All @@ -43,7 +41,7 @@ export function getMetadata(context: AppLoadContext) {
owner: 'edmundhung',
repo: 'conform',
ref: branch,
language: getLanguage(branch),
language: getLanguage(context.env.LANGUAGE),
};
}

Expand All @@ -68,7 +66,14 @@ export const getDocPath = (context: AppLoadContext) => {
return docPath;
};

export function getLanguage(code: string): Language {
export function getLanguageCode(url: string): string | undefined {
const { hostname } = new URL(url);
const language = allLanguages.find((lang) => lang.domain === hostname);

return language?.code;
}

export function getLanguage(code: string | undefined): Language {
const language = allLanguages.find((lang) => lang.code === code);
return language ?? allLanguages[0]; // default to English
}
Expand Down
1 change: 1 addition & 0 deletions guide/remix.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import '@cloudflare/workers-types';

interface Env {
ENVIRONMENT?: 'development';
LANGUAGE?: string;
GITHUB_ACCESS_TOKEN?: string;
CF_PAGES_BRANCH?: string;
CACHE: KVNamespace;
Expand Down
2 changes: 2 additions & 0 deletions guide/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { logDevReady } from '@remix-run/cloudflare';
import { createPagesFunctionHandler } from '@remix-run/cloudflare-pages';
import * as build from '@remix-run/dev/server-build';
import { getLanguageCode } from '~/util';

if (process.env.NODE_ENV === 'development') {
logDevReady(build);
Expand All @@ -11,6 +12,7 @@ export const onRequest = createPagesFunctionHandler({
getLoadContext: (context) => ({
env: {
CF_PAGES_BRANCH: 'main',
LANGUAGE: getLanguageCode(context.request.url),
...context.env,
},
waitUntil(promise: Promise<unknown>) {
Expand Down

0 comments on commit 09142b1

Please sign in to comment.