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

Commit abde929

Browse files
authored
Support Multiple Languages Through Language API (#27)
* Add language support to language api * Improve Get Method check
1 parent c6ac26e commit abde929

File tree

7 files changed

+108
-137
lines changed

7 files changed

+108
-137
lines changed

‎data/data.json renamed to ‎data/language/en.json

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -483,60 +483,6 @@
483483
}
484484
]
485485
},
486-
{
487-
"name": "flow",
488-
"description": "A git extension for feature branch workflow",
489-
"category": "Extensions",
490-
"keywords": "extensions flow init feature release hotfix start finish",
491-
"isAdvanced": false,
492-
"usage": [
493-
{
494-
"isAdvanced": false,
495-
"code": "flow init",
496-
"description": "Initialize flow inside an existing repository"
497-
},
498-
{
499-
"isAdvanced": false,
500-
"code": "flow feature start [name]",
501-
"description": "Create a new branch from latest develop as feature/feature-name"
502-
},
503-
{
504-
"isAdvanced": false,
505-
"code": "flow feature finish [name]",
506-
"description": "Merges feature-name into develop and removes branch"
507-
},
508-
{
509-
"isAdvanced": false,
510-
"code": "flow feature publish [name]",
511-
"description": "Publishes a feature to the remote"
512-
},
513-
{
514-
"isAdvanced": false,
515-
"code": "flow feature pull origin [name]",
516-
"description": "Pull a feature from remote to your local repository"
517-
},
518-
{
519-
"isAdvanced": false,
520-
"code": "flow release start [name]",
521-
"description": "Creates a release branch from latest develop"
522-
},
523-
{
524-
"isAdvanced": false,
525-
"code": "flow release finish [name]",
526-
"description": "Merges release branch into master and develop"
527-
},
528-
{
529-
"isAdvanced": false,
530-
"code": "flow hotfix start [name]",
531-
"description": "Creates a hotfix/fix-name branch from master"
532-
},
533-
{
534-
"isAdvanced": false,
535-
"code": "flow hotfix finish [name]",
536-
"description": "Merges fix-name into master and develop"
537-
}
538-
]
539-
},
540486
{
541487
"name": "config",
542488
"description": "Get and set repository or global usage",

‎gitcheats.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const DEFAULT_LANGUAGE = "en"
2+
export const AVAILABLE_LANGUAGES = ["en"]

‎package.json

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11
{
2-
"name": "git-cheats",
3-
"version": "2.0.0",
4-
"homepage": "https://git-cheats.vercel.app/",
5-
"description": "A companion for git commands",
6-
"repository": "https://github.com/excalith/git-cheats.git",
7-
"license": "MIT",
8-
"private": false,
9-
"scripts": {
10-
"prepare": "husky install",
11-
"dev": "next dev",
12-
"build": "next build",
13-
"start": "next start",
14-
"lint": "next lint",
15-
"format": "prettier . --write"
16-
},
17-
"dependencies": {
18-
"@fontsource/fira-code": "^4.5.12",
19-
"@fontsource/rubik": "^4.5.11",
20-
"bootstrap": "^5.2.3",
21-
"next": "13.0.6",
22-
"next-global-css": "^1.3.1",
23-
"react": "18.2.0",
24-
"react-dom": "18.2.0",
25-
"react-icons": "^4.7.1",
26-
"react-spinners": "^0.13.7",
27-
"swr": "^1.3.0"
28-
},
29-
"devDependencies": {
30-
"eslint": "<8.29.0",
31-
"eslint-config-next": "13.0.6",
32-
"eslint-config-prettier": "^8.5.0",
33-
"eslint-plugin-json": "^3.1.0",
34-
"eslint-plugin-prettier": "^4.2.1",
35-
"husky": "^8.0.2",
36-
"lint-staged": "^13.1.0",
37-
"prettier": "^2.8.0"
38-
},
39-
"browserslist": {
40-
"production": [
41-
">10%",
42-
"not dead",
43-
"not ie 11",
44-
"not op_mini all"
45-
],
46-
"development": [
47-
"last 1 chrome version",
48-
"last 1 firefox version",
49-
"last 1 safari version"
50-
]
51-
},
52-
"lint-staged": {
53-
"*.{js,jsx,ts,tsx}": [
54-
"prettier --write",
55-
"eslint --fix",
56-
"eslint data/data.json"
57-
]
58-
},
59-
"husky": {
60-
"hooks": {
61-
"pre-commit": "node ./"
62-
}
63-
}
2+
"name": "git-cheats",
3+
"version": "2.0.0",
4+
"homepage": "https://git-cheats.vercel.app/",
5+
"description": "A companion for git commands",
6+
"repository": "https://github.com/excalith/git-cheats.git",
7+
"license": "MIT",
8+
"private": false,
9+
"scripts": {
10+
"prepare": "husky install",
11+
"dev": "next dev",
12+
"build": "next build",
13+
"start": "next start",
14+
"lint": "next lint",
15+
"format": "prettier . --write"
16+
},
17+
"dependencies": {
18+
"@fontsource/fira-code": "^4.5.12",
19+
"@fontsource/rubik": "^4.5.11",
20+
"bootstrap": "^5.2.3",
21+
"next": "13.0.6",
22+
"next-global-css": "^1.3.1",
23+
"react": "18.2.0",
24+
"react-dom": "18.2.0",
25+
"react-icons": "^4.7.1",
26+
"react-spinners": "^0.13.7",
27+
"swr": "^1.3.0"
28+
},
29+
"devDependencies": {
30+
"eslint": "<8.29.0",
31+
"eslint-config-next": "13.0.6",
32+
"eslint-config-prettier": "^8.5.0",
33+
"eslint-plugin-json": "^3.1.0",
34+
"eslint-plugin-prettier": "^4.2.1",
35+
"husky": "^8.0.2",
36+
"lint-staged": "^13.1.0",
37+
"prettier": "^2.8.0"
38+
},
39+
"browserslist": {
40+
"production": [
41+
">10%",
42+
"not dead",
43+
"not ie 11",
44+
"not op_mini all"
45+
],
46+
"development": [
47+
"last 1 chrome version",
48+
"last 1 firefox version",
49+
"last 1 safari version"
50+
]
51+
},
52+
"lint-staged": {
53+
"*.{js,jsx,ts,tsx}": [
54+
"prettier --write",
55+
"eslint --fix",
56+
"eslint data/language/*.json"
57+
]
58+
},
59+
"husky": {
60+
"hooks": {
61+
"pre-commit": "node ./"
62+
}
63+
}
6464
}

‎pages/_app.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import "../styles/code.css"
77
import "../styles/footer.css"
88

99
function MyApp({ Component, pageProps }) {
10-
console.log(
11-
"Hey there!\nInstead of peeking into the code,\nyou can find the whole project on\nhttps://github.com/excalith/git-cheats"
12-
)
1310
return <Component {...pageProps} />
1411
}
1512

‎pages/api/language.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2+
// https://vercel.com/guides/loading-static-file-nextjs-api-route
3+
import path from "path"
4+
import { promises as fs } from "fs"
5+
import { DEFAULT_LANGUAGE, AVAILABLE_LANGUAGES } from "../../gitcheats.config"
6+
7+
export default async function handler(req, res) {
8+
// Only GET method is allowed
9+
if (req.method != "GET") {
10+
res.status(405).end()
11+
return
12+
}
13+
14+
// Get requested language
15+
let queryLang = req.query.lang
16+
17+
// Set default language
18+
let language = DEFAULT_LANGUAGE
19+
20+
// Check query language if valid. Else, continue with default language
21+
if (queryLang != "undefined" && AVAILABLE_LANGUAGES.includes(queryLang)) {
22+
language = queryLang
23+
}
24+
25+
// Find the absolute path of the json directory
26+
const dataDirectory = path.join(process.cwd(), "data/language")
27+
28+
// Read the json data file data.json
29+
const fileContents = await fs.readFile(
30+
dataDirectory + "/" + language + ".json",
31+
"utf8"
32+
)
33+
34+
// Return the content of the data file in json format
35+
res.status(200).json(fileContents)
36+
}

‎pages/api/staticdata.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎pages/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import Card from "../components/Card"
77
import Footer from "../components/Footer"
88
import Loader from "../components/Loader"
99

10-
const fetcher = (url) => fetch(url).then((res) => res.json())
10+
// const fetcher = (url) => fetch(url).then((res) => res.json())
11+
12+
const fetcher = (url, lang) =>
13+
fetch(`${url}?lang=${lang}`).then((res) => res.json())
1114

1215
export default function Home() {
13-
//Set up SWR to run the fetcher function when calling "/api/staticdata"
14-
//There are 3 possible states: (1) loading when data is null (2) ready when the data is returned (3) error when there was an error fetching the data
15-
const { data, error } = useSWR("/api/staticdata", fetcher)
16+
const router = useRouter()
17+
const { lang } = router.query
18+
const { data, error } = useSWR(["/api/language", lang], fetcher)
19+
1620
const [search, setSearch] = useState("")
1721
const [isAdvanced, setAdvanced] = useState(false)
1822
const { query: filterQuery } = useRouter()

0 commit comments

Comments
 (0)