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

[zh-cn] refactor Using_custom_elements #17038

Merged
merged 18 commits into from
Nov 25, 2023
Merged

[zh-cn] refactor Using_custom_elements #17038

merged 18 commits into from
Nov 25, 2023

Conversation

YieldRay
Copy link
Contributor

The existing translation is outdated and contains incorrect translation, so I translated the latest English document, which significantly reduce misleading to reader.

Additional details

Source:
https://github.com/mdn/content/blob/main/files/en-us/web/api/web_components/using_custom_elements/index.md?plain=1

The existing translation is outdated and contains incorrect translation, so I translated the latest English document, which significantly reduce misleading to reader.
@YieldRay YieldRay requested a review from a team as a code owner November 20, 2023 05:52
@YieldRay YieldRay requested review from t7yang and removed request for a team November 20, 2023 05:52
@YieldRay
Copy link
Contributor Author

Dear reviewer, some term has no standard translation so I keep them as they are (mainly: autonomous custom element customized built-in elements shadow root), lines of the translated markdown file are consistent with the source file. Feel free to modify it.

@github-actions github-actions bot added the l10n-zh Issues related to Chinese content. label Nov 20, 2023
YieldRay and others added 2 commits November 20, 2023 13:54
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

github-actions bot commented Nov 20, 2023

Preview URLs

Flaws (1)

URL: /zh-CN/docs/Web/API/Web_components/Using_custom_elements
Title: 使用自定义元素
Flaw count: 1

  • macros:
    • /zh-CN/docs/Web/API/HTMLUListElement does not exist but fell back to /en-US/docs/Web/API/HTMLUListElement
External URLs (6)

URL: /zh-CN/docs/Web/API/Web_components/Using_custom_elements
Title: 使用自定义元素

(comment last updated: 2023-11-25 08:11:16)

@yin1999 yin1999 self-assigned this Nov 21, 2023
@yin1999 yin1999 removed the request for review from t7yang November 21, 2023 08:51

{{domxref("CustomElementRegistry.define()")}} 方法用来注册一个 custom element,该方法接受以下参数:
- **Customized built-in elements** 继承自标准的 HTML 元素,例如 {{domxref("HTMLImageElement")}} 或 {{domxref("HTMLParagraphElement")}}。它们的实现定义了标准元素的行为。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **Customized built-in elements** 继承自标准的 HTML 元素,例如 {{domxref("HTMLImageElement")}} 或 {{domxref("HTMLParagraphElement")}}。它们的实现定义了标准元素的行为。
- **自定义内置元素**继承自标准的 HTML 元素,例如 {{domxref("HTMLImageElement")}} 或 {{domxref("HTMLParagraphElement")}}。它们的实现定义了标准元素的行为。


{{domxref("CustomElementRegistry.define()")}} 方法用来注册一个 custom element,该方法接受以下参数:
- **Customized built-in elements** 继承自标准的 HTML 元素,例如 {{domxref("HTMLImageElement")}} 或 {{domxref("HTMLParagraphElement")}}。它们的实现定义了标准元素的行为。
- **Autonomous custom elements** 继承自 HTML 元素基类 {{domxref("HTMLElement")}}。你必须从头开始实现它们的行为。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Candidate translation

/cc @jasonren0403 what's your opinion?

Suggested change
- **Autonomous custom elements** 继承自 HTML 元素基类 {{domxref("HTMLElement")}}。你必须从头开始实现它们的行为。
- **独立自定义元素**继承自 HTML 元素基类 {{domxref("HTMLElement")}}。你必须从头开始实现它们的行为。

Copy link
Contributor

@jasonren0403 jasonren0403 Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer using this too 👍


作为示例,我们可以像这样定义一个叫做 [word-count](https://mdn.github.io/web-components-examples/word-count-web-component/) 的 custom element:
自定义元素作为一个[类](/docs/Web/JavaScript/Reference/Classes)来实现,该类可以扩展 {{domxref("HTMLElement")}}(对于 autonomous elements)或者你想要定制的接口(对于 customized built-in elements)。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
自定义元素作为一个[](/docs/Web/JavaScript/Reference/Classes)来实现,该类可以扩展 {{domxref("HTMLElement")}}(对于 autonomous elements)或者你想要定制的接口(对于 customized built-in elements)。
自定义元素作为一个[](/docs/Web/JavaScript/Reference/Classes)来实现,该类可以扩展 {{domxref("HTMLElement")}}(即独立元素的情况)或者你想要定制的接口(即自定义内置元素的情况)。

@YieldRay YieldRay requested a review from yin1999 November 24, 2023 12:09
Copy link
Member

@yin1999 yin1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid update. Please check those comments before we can merge this :)

Comment on lines 71 to 83
console.log("Custom element added to page.");
}

...
disconnectedCallback() {
console.log("Custom element removed from page.");
}

adoptedCallback() {
console.log("Custom element moved to new page.");
}

attributeChangedCallback(name, oldValue, newValue) {
console.log(`Attribute ${name} has changed.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you translate those texts. This example is not a part of live examples (which are not localized). So it's better to localize them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it.

...
attributeChangedCallback(name, oldValue, newValue) {
console.log(
`Attribute ${name} has changed from ${oldValue} to ${newValue}.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

translate this

```

上述代码片段中,类的构造函数{{jsxref("Classes.constructor","constructor")}}总是先调用[`super()`](/zh-CN/docs/Web/JavaScript/Reference/Operators/super)来建立正确的原型链继承关系。
请注意,如果元素的 `HTML` 声明包含一个被观察的属性,那么在元素的声明首次解析时,`attributeChangedCallback()` 将被调用,即在属性被初始化后。因此,在以下示例中,即使属性再也没有被更改,当 DOM 被解析时,`attributeChangedCallback()` 也会被调用:(译注:即每个直接在 HTML 中声明的且被观察的属性会立即触发 `attributeChangedCallback()` 一次)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove all the translation note, if the body is not will written, please update the original English content.

Suggested change
请注意,如果元素的 `HTML` 声明包含一个被观察的属性,那么在元素的声明首次解析时,`attributeChangedCallback()` 将被调用,即在属性被初始化后。因此,在以下示例中,即使属性再也没有被更改,当 DOM 被解析时,`attributeChangedCallback()` 也会被调用:(译注:即每个直接在 HTML 中声明的且被观察的属性会立即触发 `attributeChangedCallback()` 一次)
请注意,如果元素的 `HTML` 声明包含一个被观察的属性,那么在元素的声明首次解析时,`attributeChangedCallback()` 将被调用,即在属性被初始化后。因此,在以下示例中,即使属性再也没有被更改,当 DOM 被解析时,`attributeChangedCallback()` 也会被调用:


## 示例

在本指南的其余部分,我们将看一些示例自定义元素。你可以在 [web-components-examples](https://github.com/mdn/web-components-examples) 存储库中找到所有这些示例的源代码,以及更多示例,并且你可以在 <https://mdn.github.io/web-components-examples/> 上实时查看它们。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use "仓库" rather than "存储库" (align with other documents)

Suggested change
在本指南的其余部分,我们将看一些示例自定义元素。你可以在 [web-components-examples](https://github.com/mdn/web-components-examples) 存储库中找到所有这些示例的源代码,以及更多示例,并且你可以在 <https://mdn.github.io/web-components-examples/> 上实时查看它们。
在本指南的其余部分,我们将看一些示例自定义元素。你可以在 [web-components-examples](https://github.com/mdn/web-components-examples) 仓库中找到所有这些示例的源代码,以及更多示例,并且你可以在 <https://mdn.github.io/web-components-examples/> 上实时查看它们。

- `disconnectedCallback`:当 custom element 从文档 DOM 中删除时,被调用。
- `adoptedCallback`:当 custom element 被移动到新的文档时,被调用。
- `attributeChangedCallback`: 当 custom element 增加、删除、修改自身属性时,被调用。
到目前为止,我们只看到了一个生命周期回调的实际应用:`connectedCallback()`。在最后一个例子中,`<custom-square>`,我们将看到一些其他的回调。`<custom-square>` 独立自定义元素绘制一个正方形,其大小和颜色由两个名为 `"size"` 和 `"color"` 的属性确定。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
到目前为止,我们只看到了一个生命周期回调的实际应用:`connectedCallback()`在最后一个例子中,`<custom-square>`,我们将看到一些其他的回调。`<custom-square>` 独立自定义元素绘制一个正方形,其大小和颜色由两个名为 `"size"``"color"` 的属性确定。
到目前为止,我们只看到了一个生命周期回调的实际应用:`connectedCallback()`在最后一个示例——`<custom-square>`——中,我们将看到一些其他的回调。`<custom-square>` 独立自定义元素绘制一个正方形,其大小和颜色由两个名为 `"size"``"color"` 的属性确定。

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@YieldRay
Copy link
Contributor Author

May be we need to update translation for Using_shadow_DOM one day.

Copy link
Member

@yin1999 yin1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yin1999 yin1999 merged commit c0af68d into mdn:main Nov 25, 2023
7 checks passed
@yin1999
Copy link
Member

yin1999 commented Nov 25, 2023

May be we need to update translation for Using_shadow_DOM one day.

Feel free to update it whenever you have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
l10n-zh Issues related to Chinese content.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants