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]: Update translation of String.*WellFormed() #15173

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -5,7 +5,7 @@ slug: Web/JavaScript/Reference/Global_Objects/String/isWellFormed

{{JSRef}}

{{jsxref("String")}} 值的 **`isWellFormed()`** 方法返回一个表示该字符串是否包含[单独的代理项](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_字符、unicode_码位和字素簇(grapheme_clusters))的布尔值。
{{jsxref("String")}} 值的 **`isWellFormed()`** 方法返回一个表示该字符串是否包含[单独代理项](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_字符、unicode_码位和字素簇)的布尔值。

## 语法

Expand All @@ -15,24 +15,24 @@ isWellFormed()

### 返回值

如果字符串不包含单独的代理项,返回 `true`,否则返回 `false`。
如果字符串不包含单独代理项,返回 `true`,否则返回 `false`。

## 描述

JavaScript 中的字符串是 UTF-16 编码的。UTF-16 编码中有*代理对*的概念,这一概念在 [UTF-16 字符、Unicode 码位和字素簇(grapheme clusters)](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_字符、unicode_码位和字素簇(grapheme_clusters))部分有详细介绍。
JavaScript 中的字符串是 UTF-16 编码的。UTF-16 编码中有*代理对*的概念,这一概念在 [UTF-16 字符、Unicode 码位和字素簇(grapheme clusters)](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_字符、unicode_码位和字素簇)部分有详细介绍。

`isWellFormed()` 让你能够测试一个字符串是否是格式正确的(即不包含单独的代理项)。由于引擎能够直接访问字符串的内部表示,与自定义实现相比 `isWellFormed()` 更高效。如果你需要将字符串转换为格式正确的字符串,可以使用 {{jsxref("String/toWellFormed", "toWellFormed()")}} 方法。`isWellFormed()` 让你可以对格式正确和格式错误的字符串进行不同的处理,比如抛出一个错误或将其标记为无效。
`isWellFormed()` 让你能够测试一个字符串是否是格式正确的(即不包含单独代理项)。由于引擎能够直接访问字符串的内部表示,与自定义实现相比 `isWellFormed()` 更高效。如果你需要将字符串转换为格式正确的字符串,可以使用 {{jsxref("String/toWellFormed", "toWellFormed()")}} 方法。`isWellFormed()` 让你可以对格式正确和格式错误的字符串进行不同的处理,比如抛出一个错误或将其标记为无效。

## 示例

### 使用 isWellFormed()

```js
const strings = [
// 单独的高位代理
// 单独的前导代理
"ab\uD800",
"ab\uD800c",
// 单独的低位代理
// 单独的后尾代理
"\uDFFFab",
"c\uDFFFab",
// 格式正确
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ slug: Web/JavaScript/Reference/Global_Objects/String/toWellFormed

{{JSRef}}

{{jsxref("String")}} 值的 **`toWellFormed()`** 方法返回一个新字符串,原字符串中所有[单独的代理项](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_字符、unicode_码位和字素簇(grapheme_clusters))在新字符串中会被替换为 Unicode 替换字符 U+FFFD。
{{jsxref("String")}} **`toWellFormed()`** 方法返回一个字符串,其中该字符串的所有[单独代理项](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_字符、unicode_码位和字素簇)都被替换为 Unicode 替换字符 U+FFFD。

## 语法

Expand All @@ -15,26 +15,26 @@ toWellFormed()

### 返回值

新的字符串是原字符串的一个拷贝,其中所有的单独的代理项被替换为 Unicode 替换字符 U+FFFD。如果 `str` [是格式正确的](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/isWellFormed),仍然会返回一个新字符串(本质上是 `str` 的一个拷贝)。
新的字符串是原字符串的一个拷贝,其中所有的单独代理项被替换为 Unicode 替换字符 U+FFFD。如果 `str` [是格式正确的](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/isWellFormed),仍然会返回一个新字符串(本质上是 `str` 的一个拷贝)。

## 描述

JavaScript 中的字符串是 UTF-16 编码的。UTF-16 编码中有*代理对*的概念,这一概念在 [UTF-16 字符、Unicode 码位和字素簇(grapheme clusters)](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_字符、unicode_码位和字素簇(grapheme_clusters))部分有详细介绍。
JavaScript 中的字符串是 UTF-16 编码的。UTF-16 编码中有*代理对*的概念,这一概念在 [UTF-16 字符、Unicode 码位和字素簇](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#utf-16_字符、unicode_码位和字素簇)部分有详细介绍。

`toWellFormed()` 迭代字符串的码元,并将任何单独的代理项替换为 [Unicode 替换字符](<https://zh.wikipedia.org/wiki/特殊_(Unicode區段)>) U+FFFD `�`。这确保了返回的字符串格式正确并可用于期望正确格式字符串的函数,比如 {{jsxref("encodeURI")}}。由于引擎能够直接访问字符串的内部表示,与自定义实现相比 `toWellFormed()` 更高效。
`toWellFormed()` 迭代字符串的码元,并将任何单独代理项替换为 [Unicode 替换字符](<https://zh.wikipedia.org/wiki/特殊_(Unicode區段)>) U+FFFD `�`。这确保了返回的字符串格式正确并可用于期望正确格式字符串的函数,比如 {{jsxref("encodeURI")}}。由于引擎能够直接访问字符串的内部表示,与自定义实现相比 `toWellFormed()` 更高效。
Copy link
Member

Choose a reason for hiding this comment

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

The \U+FFFD Unicode character makes the gh command fail: cli/go-gh#127. I will review the PR locally. Do not worry about the failed CIs.


当在某些上下文中使用格式不正确的字符串时,例如 {{domxref("TextEncoder")}},它们会自动转换为使用相同替换字符的格式正确的字符串。当单独的代理项被呈现时,它们也会呈现为替换字符(一个带有问号的钻石形状)。
当在某些上下文中使用格式不正确的字符串时,例如 {{domxref("TextEncoder")}},它们会自动转换为使用相同替换字符的格式正确的字符串。当单独代理项被呈现时,它们也会呈现为替换字符(一个带有问号的钻石形状)。

## 示例

### 使用 toWellFormed()

```js
const strings = [
// 单独的高位代理
// 单独的前导代理
"ab\uD800",
"ab\uD800c",
// 单独的低位代理
// 单独的后尾代理
"\uDFFFab",
"c\uDFFFab",
// 格式正确
Expand Down
Loading