Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement language as state instead of cookie (#1785)
close #1365 close #1662 close #1692 close #1530 - Detect language from URL instead of cookie. Steps to verify this. - In tab 1, choose a task in language en. - Open tab 2, change the language to a different one, and complete 1 task. - Back to tab 1 to finish the pending task. Verify that the next task and the UI are still in English. This change is really **critical**, so be careful when deploying to production, better to test it in staging with our community first
- Loading branch information
Showing
15 changed files
with
111 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { useRouter } from "next/router"; | ||
|
||
export const useCurrentLocale = () => useRouter().locale || "en"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,46 @@ | ||
import { TaskType } from "src/types/Task"; | ||
|
||
export type RouteQuery = Record<string, string | number | boolean | undefined>; | ||
|
||
export const stringifyQuery = (query: RouteQuery | undefined) => { | ||
if (!query) { | ||
return ""; | ||
} | ||
|
||
const filteredQuery = Object.fromEntries(Object.entries(query).filter(([, value]) => value !== undefined)) as Record< | ||
string, | ||
string | ||
>; | ||
|
||
return new URLSearchParams(filteredQuery).toString(); | ||
}; | ||
|
||
const createRoute = (path: string, query?: RouteQuery) => { | ||
if (!query) { | ||
return path; | ||
} | ||
|
||
return `${path}?${stringifyQuery(query)}`; | ||
}; | ||
|
||
export const ROUTES = { | ||
ADMIN_MESSAGE_DETAIL: (id: string) => `/admin/messages/${id}`, | ||
MESSAGE_DETAIL: (id: string) => `/messages/${id}`, | ||
}; | ||
|
||
export type QueryWithLang<T extends RouteQuery | undefined = undefined> = T extends undefined | ||
? { lang: string } | ||
: T & { lang: string }; | ||
|
||
const withLang = | ||
<T extends RouteQuery | undefined = undefined>(path: string, q?: T) => | ||
(query: QueryWithLang<T>) => { | ||
return createRoute(path, { ...q, ...query }); | ||
}; | ||
|
||
export const API_ROUTES = { | ||
NEW_TASK: (type: TaskType, query: QueryWithLang) => createRoute(`/api/new_task/${type}`, query), | ||
UPDATE_TASK: "/api/update_task", | ||
AVAILABLE_TASK: withLang("/api/available_tasks"), | ||
RECENT_MESSAGES: withLang("/api/messages"), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.