-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i18n(zh-cn): translate
features/os-info.mdx
and `features/http-clie…
…nt.mdx` (#1887) * translate features/os-info.mdx * translate features/http-client.mdx * Update src/content/docs/zh-cn/features/os-info.mdx Co-authored-by: DK Liao <dklassic@gmail.com> * Update src/content/docs/zh-cn/features/http-client.mdx Co-authored-by: DK Liao <dklassic@gmail.com> * Update src/content/docs/zh-cn/features/http-client.mdx Co-authored-by: DK Liao <dklassic@gmail.com> --------- Co-authored-by: DK Liao <dklassic@gmail.com>
- Loading branch information
Showing
2 changed files
with
197 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
--- | ||
title: HTTP 客户端 | ||
description: 访问用 Rust 编写的 HTTP 客户端。 | ||
--- | ||
|
||
import PluginLinks from '@components/PluginLinks.astro'; | ||
import { Tabs, TabItem } from '@astrojs/starlight/components'; | ||
import CommandTabs from '@components/CommandTabs.astro'; | ||
|
||
<PluginLinks plugin="http" /> | ||
|
||
使用 HTTP 插件发起 HTTP 请求。 | ||
|
||
## 设置 | ||
|
||
请安装 http 插件。 | ||
|
||
<Tabs> | ||
<TabItem label="自动"> | ||
|
||
使用项目的包管理器来添加依赖: | ||
|
||
<CommandTabs npm="npm run tauri add http" | ||
yarn="yarn run tauri add http" | ||
pnpm="pnpm tauri add http" | ||
cargo="cargo tauri add http" /> | ||
|
||
</TabItem> | ||
<TabItem label="手动"> | ||
|
||
1. 运行 `cargo add tauri-plugin-http` 命令,将插件添加到项目的 cargo .toml 依赖中。 | ||
|
||
2. 修改 lib.rs 来初始化插件。 | ||
|
||
```rust ins={6} | ||
// lib.rs | ||
#[cfg_attr(mobile, tauri::mobile_entry_point)] | ||
pub fn run() { | ||
tauri::Builder::default() | ||
// Initialize the plugin | ||
.plugin(tauri_plugin_http::init()) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
} | ||
``` | ||
|
||
3. 如果你想用 JavaScript 发送 http 请求,还需要安装 npm 包。 | ||
|
||
<CommandTabs | ||
npm="npm install @tauri-apps/plugin-http" | ||
yarn="yarn add @tauri-apps/plugin-http" | ||
pnpm="pnpm add @tauri-apps/plugin-http" | ||
/> | ||
|
||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
## 用法 | ||
|
||
http 插件既有 JavaScript API 版本,也有 Rust [reqwest](https://docs.rs/reqwest/) 重新导出的版本。 | ||
|
||
### JavaScript | ||
|
||
1. 为了保证你的应用安全,请配置允许的作用域。更多信息请参阅 [JavaScript API 参考](https://beta.tauri.app/2/reference/js/http/#security)。 | ||
|
||
```json | ||
//tauri.conf.json | ||
{ | ||
"plugins": { | ||
"http": { | ||
"scope": ["http://my.api.host/*"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
2. 发送请求 | ||
|
||
```js | ||
import { fetch } from '@tauri-apps/plugin-http'; | ||
|
||
// Send a GET request | ||
const response = await fetch('http://my.api.host/data.json', { | ||
method: 'GET', | ||
}); | ||
console.log(response.status); // e.g. 200 | ||
console.log(response.statusText); // e.g. "OK" | ||
``` | ||
|
||
:::note | ||
|
||
当前的 `fetch` 方法是一个 Rust 后端 API。 它试图与 [`fetch` Web API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) 尽可能接近和兼容。 | ||
|
||
::: | ||
|
||
### Rust | ||
|
||
在 Rust 中,你可以利用插件重新导出的 `reqwest` 包。更多细节请参考 [reqwest 文档](https://docs.rs/reqwest/)。 | ||
|
||
```rust | ||
use tauri_plugin_http::reqwest; | ||
|
||
let res = reqwest::get("http://my.api.host/data.json").await; | ||
println!("{:?}", res.status()); // e.g. 200 | ||
println!("{:?}", res.text().await); // e.g Ok("{ Content }") | ||
``` |
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,90 @@ | ||
--- | ||
title: 操作系统信息 | ||
description: 查看操作系统信息。 | ||
--- | ||
|
||
import PluginLinks from '@components/PluginLinks.astro'; | ||
import { Tabs, TabItem } from '@astrojs/starlight/components'; | ||
import CommandTabs from '@components/CommandTabs.astro'; | ||
|
||
<PluginLinks plugin="os" /> | ||
|
||
使用操作系统信息插件读取操作系统信息。 | ||
|
||
## 设置 | ||
|
||
安装操作系统信息插件开始。 | ||
|
||
<Tabs> | ||
<TabItem label="自动"> | ||
|
||
使用项目的包管理器来添加依赖: | ||
|
||
<CommandTabs npm="npm run tauri add os" | ||
yarn="yarn run tauri add os" | ||
pnpm="pnpm tauri add os" | ||
cargo="cargo tauri add os" /> | ||
|
||
</TabItem> | ||
<TabItem label="手动"> | ||
|
||
1. 运行 `cargo add tauri-plugin-os` 命令,将插件添加到项目的 cargo .toml 依赖中。 | ||
|
||
2. 修改 lib.rs 来初始化插件。 | ||
|
||
```rust | ||
// lib.rs | ||
#[cfg_attr(mobile, tauri::mobile_entry_point)] | ||
pub fn run() { | ||
tauri::Builder::default() | ||
// Initialize the plugin | ||
.plugin(tauri_plugin_os::init()) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
} | ||
``` | ||
|
||
3. 如果你想在 JavaScript 中使用,还需要安装 npm 包。 | ||
|
||
<CommandTabs | ||
npm="npm install @tauri-apps/plugin-os" | ||
yarn="yarn add @tauri-apps/plugin-os" | ||
pnpm="pnpm add @tauri-apps/plugin-os" | ||
/> | ||
|
||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
## 用法 | ||
|
||
通过这个插件,您可以查询当前操作系统的多个信息。请参阅 [JavaScript API](/references/v2/js/os/) 或 [Rust API](https://docs.rs/tauri-plugin-os/) 参考资料中的所有可用函数。 | ||
|
||
{/* TODO: Link to which language to use, frontend vs. backend guide when it's made */} | ||
|
||
#### 示例:操作系统平台 | ||
|
||
`platform` 返回一个描述使用的特定操作系统的字符串。该值在编译时设置。可能的值有 `linux`、`macos`、`ios`、`freebsd`、`dragonfly`、`netbsd`、`openbsd`、`solaris`、`android`、`windows`。 | ||
|
||
<Tabs> | ||
<TabItem label="JavaScript"> | ||
|
||
```js | ||
import { platform } from '@tauri-apps/plugin-os'; | ||
|
||
const currentPlatform = await platform(); | ||
console.log(currentPlatform); | ||
// Prints "windows" to the console | ||
``` | ||
|
||
</TabItem> | ||
<TabItem label="Rust"> | ||
|
||
```rust | ||
let platform = tauri_plugin_os::platform(); | ||
println!("Platform: {}", platform); | ||
// Prints "windows" to the terminal | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> |