-
Notifications
You must be signed in to change notification settings - Fork 179
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
[WIP] Attempted conversion of i18n package #12201
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
|
@@ -23,6 +23,12 @@ import { | |
Fragment, | ||
} from '@googleforcreators/react'; | ||
import PropTypes from 'prop-types'; | ||
import type React from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { Mapping } from './types'; | ||
|
||
/** @typedef {import('react').ReactNode} ReactNode */ | ||
/** @typedef {import('react').ReactElement} ReactElement */ | ||
|
@@ -52,18 +58,22 @@ const VOID_ELEMENTS = [ | |
* @param {?Object.<string, ReactElement>} mapping Map of tag names to React components. | ||
* @return {ReactNode} Transformed node. | ||
*/ | ||
export function transformNode(node, mapping = {}) { | ||
const { childNodes, localName, nodeType, textContent } = node; | ||
export function transformNode( | ||
node: ChildNode, | ||
mapping: Mapping = {} | ||
): React.ReactNode { | ||
const { firstChild, nodeType, textContent } = node; | ||
if (Node.TEXT_NODE === nodeType) { | ||
return textContent; | ||
} | ||
|
||
const children = node.hasChildNodes() | ||
? [...childNodes].map((child) => transform(child, mapping)) | ||
: null; | ||
const children = node.hasChildNodes() ? transform(firstChild, mapping) : null; | ||
|
||
if (localName in mapping) { | ||
return cloneElement(mapping[localName], null, children); | ||
if ('localName' in node) { | ||
const { localName } = node as Element; | ||
if (localName in mapping) { | ||
return cloneElement(mapping[localName], undefined, children); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the life of me, I can't figure out how to get TS to accept the type of this one? I tried everything, but I can't match the types to any of the overloads available. And similarly for |
||
} | ||
} | ||
|
||
return createElement(localName, null, children); | ||
|
@@ -77,17 +87,21 @@ export function transformNode(node, mapping = {}) { | |
* @param {?Object.<string, ReactElement>} mapping Map of tag names to React components. | ||
* @return {ReactNode[]} List of transformed nodes. | ||
*/ | ||
function transform(node, mapping = {}) { | ||
const result = []; | ||
|
||
do { | ||
function transform(node: ChildNode | null, mapping: Mapping = {}) { | ||
const result: Array<React.ReactNode> = []; | ||
while (node) { | ||
result.push(transformNode(node, mapping)); | ||
node = node.nextSibling; | ||
} while (node !== null); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
interface TranslateWithMarkupProps { | ||
mapping: Mapping; | ||
children: string; | ||
} | ||
|
||
/** | ||
* Component to facilitate translation of strings containing markup. | ||
* | ||
|
@@ -99,10 +113,13 @@ function transform(node, mapping = {}) { | |
* @see https://github.com/googleforcreators/web-stories-wp/issues/1578 | ||
* @param {Object} props Component props. | ||
* @param {?Object.<string, ReactElement>} props.mapping Map of tag names to React components. | ||
* @param {ReactNode[]|string} props.children Children / string to parse. | ||
* @param {string} props.children Children / string to parse. | ||
* @return {ReactNode[]} Transformed children. | ||
*/ | ||
function TranslateWithMarkup({ mapping = {}, children }) { | ||
function TranslateWithMarkup({ | ||
mapping = {}, | ||
children, | ||
}: TranslateWithMarkupProps) { | ||
//Ensure all Object keys are lowercase as the DOMParser converts tag names to lowercase. | ||
mapping = Object.fromEntries( | ||
Object.entries(mapping).map(([k, v]) => [k.toLowerCase(), v]) | ||
|
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,33 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
type DomainMetadata = { | ||
domain: string; | ||
lang: string; | ||
plural_forms: string | (() => string); | ||
}; | ||
|
||
type Translation = [string, string]; | ||
|
||
type LocaleDomain = Record<string, DomainMetadata | Translation>; | ||
|
||
type LocaleData = Record<string, LocaleDomain>; | ||
|
||
export type LocaleDataWrapper = { | ||
locale_data: LocaleData; | ||
}; | ||
|
||
export type Mapping = Record<string, React.ReactElement>; |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.shared.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"declarationDir": "dist-types" | ||
}, | ||
"references": [{ "path": "../react" }], | ||
"include": ["src/**/*"] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am pretty sure this old code is wrong, no? It loops over all the child nodes and transforms each, but
transformNode
transform both the given node and all its following siblings, so this old code duplicates a lot of nodes, no?Well, at least the type system told me that the
children
variable had typeReactNode[][]
, which definitely seemed wrong.