Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/contribution repository differentiation #2535

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useEffect } from "react";
import attribution from "../../../../documentation/output/attribution.json";
import { doesLocalePageExist } from "../layout/doesLocalePageExist";
import { isEnglishPath } from "../layout/isEnglishPath";

interface ContributorsProps {
i: (string) => string;
Expand All @@ -19,11 +21,16 @@ const Section = (props: { children: any; className?: string; sKey: string }) =>
export const Contributors = (props: ContributorsProps) => {
const attrPath = props.path.replace("/packages/documentation/", "");
const page = attribution[attrPath];
const shouldRedirectToLocalization = !isEnglishPath() && doesLocalePageExist()

// https://github.com/microsoft/TypeScript-Website/blob/v2/packages/documentation/en/Advanced%20Types.md
const reposRootURL =
"https://github.com/microsoft/TypeScript-Website/blob/v2";
const repoPageURL = reposRootURL + props.path;
const reposRootURL = !shouldRedirectToLocalization ?
"https://github.com/microsoft/TypeScript-Website/blob/v2" :
"https://github.com/microsoft/TypeScript-Website-Localizations/blob/main/docs/documentation";
const path = !shouldRedirectToLocalization ?
props.path :
props.path.replace(/^.*\/packages\/documentation\/copy/g, '');
const repoPageURL = reposRootURL + path;

const d = new Date(props.lastEdited);
const dtf = new Intl.DateTimeFormat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,13 @@ import { SeoProps } from "../HeadSEO"
import { inYourLanguage } from "../../copy/inYourLanguage";
import { hasLocalStorage } from "../../lib/hasLocalStorage";
import { allFiles } from "../../__generated__/allPages"
import { getLocalePath } from "./getLocalePath";

type Props = SeoProps & {
lang: string,
children: any
}

const getLocaleVersionOfPage = () => {
// @ts-ignore
const userLocale = navigator.language || navigator.userLanguage || "en-UK"
const userLang = userLocale.split("-")[0]
const thisPaths = location.pathname.split("/")

// / -> /es
if (thisPaths.length === 0) {
return "/" + userLocale
}

const isEnglishPath = thisPaths[1].length !== 2

// /play -> /zh/play
if (isEnglishPath) {
return "/" + userLang + location.pathname
}

// /zh/play -> /es/play
thisPaths[1] = userLang
// Drop any preceding /s
if (thisPaths[thisPaths.length - 1] === "") thisPaths.pop()

return thisPaths.join("/")
}

export const LanguageRecommendations = (props: Props) => {
useEffect(() => {
// Don't mess with the mobile UI
Expand All @@ -46,15 +21,11 @@ export const LanguageRecommendations = (props: Props) => {

const suppressed = hasLocalStorage && localStorage.getItem("dont-recommend-translate")

let localePath = getLocaleVersionOfPage()
if (localePath.startsWith("/en")) {
localePath = localePath.slice(3)
}
let localePath = getLocalePath()

// Heh, ignore dt urls
if (localePath.startsWith("/dt")) return

if (localePath === "") localePath = "/"
if (localePath === location.pathname) return

const doesPageExist = allFiles.find(f => f === localePath || f + "/" === localePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { allFiles } from "../../__generated__/allPages"
import { getLocalePath } from "./getLocalePath"

export const doesLocalePageExist = (path?: string): boolean => {
const localePath = path || getLocalePath()

const doesPageExist = allFiles.find(f => f === localePath || f + "/" === localePath)
if (!doesPageExist) return false

return true
}
12 changes: 12 additions & 0 deletions packages/typescriptlang-org/src/components/layout/getLocalePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getLocaleVersionOfPage } from "./getLocaleVersionOfPage"

export const getLocalePath = () => {
let localePath = getLocaleVersionOfPage()
if (localePath.startsWith("/en")) {
localePath = localePath.slice(3)
}

if (localePath === "") localePath = "/"

return localePath
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const getLocaleVersionOfPage = () => {
// @ts-ignore
const userLocale = navigator.language || navigator.userLanguage || "en-UK"
const userLang = userLocale.split("-")[0]
const thisPaths = location.pathname.split("/")

// / -> /es
if (thisPaths.length === 0) {
return "/" + userLocale
}

const isEnglishPath = thisPaths[1].length !== 2

// /play -> /zh/play
if (isEnglishPath) {
return "/" + userLang + location.pathname
}

// /zh/play -> /es/play
thisPaths[1] = userLang
// Drop any preceding /s
if (thisPaths[thisPaths.length - 1] === "") thisPaths.pop()

return thisPaths.join("/")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const isEnglishPath = () => {
const thisPaths = location.pathname.split("/")
const isEnglishPath = thisPaths[1].length !== 2

return isEnglishPath;
};