Skip to content

Commit 8e2c54f

Browse files
authored
docs: 36-dịch-4-advanced-sveltekit-environment-variables (sveltevietnam#71)
1 parent 2b6b62c commit 8e2c54f

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

content/tutorial/04-advanced-sveltekit/06-environment-variables/01-env-static-private/README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
title: $env/static/private
33
---
44

5-
Environment variables — like API keys and database credentials — can be added to a `.env` file, and they will be made available to your application.
5+
Biến môi trường - như khóa API và thông tin đăng nhập cơ sở dữ liệu - có thể được thêm vào tệp `.env` và sẽ có hiệu lực trong ứng dụng của bạn.
66

7-
> You can also use `.env.local` or `.env.[mode]` files — see the [Vite documentation](https://vitejs.dev/guide/env-and-mode.html#env-files) for more information. Make sure you add any files containing sensitive information to your `.gitignore` file!
7+
> Bạn cũng có thể sử dụng các tệp `.env.local` hoặc `.env.[mode]` - xem [tài liệu Vite](https://vitejs.dev/guide/env-and-mode.html#env-files) để biết thêm thông tin. Hãy nhớ thêm bất kỳ tệp nào chứa thông tin nhạy cảm vào `.gitignore` của bạn!
88
>
9-
> Environment variables in `process.env` are also available via `$env/static/private`.
9+
> Biến môi trường trong `process.env` cũng có sẵn qua `$env/static/private`.
1010
11-
In this exercise, we want to allow the user to enter the website if they know the correct passphrase, using an environment variable.
11+
Trong bài tập này, bằng cách sử dụng biến môi trường, chúng ta muốn cho phép người dùng truy cập trang web nếu họ biết passphrase đúng.
1212

13-
First, in `.env`, add a new environment variable:
13+
Đầu tiên, trong `.env`, thêm một biến môi trường mới:
1414

1515
```env
1616
/// file: .env
1717
PASSPHRASE=+++"open sesame"+++
1818
```
1919

20-
Open `src/routes/+page.server.js`. Import `PASSPHRASE` from `$env/static/private` and use it inside the [form action](/tutorial/the-form-element):
20+
Mở `src/routes/+page.server.js`. Import `PASSPHRASE` từ `$env/static/private` và sử dụng nó bên trong [form action](/tutorial/the-form-element):
2121

2222
```js
2323
/// file: src/routes/+page.server.js
@@ -49,13 +49,13 @@ export const actions = {
4949
};
5050
```
5151

52-
The website is now accessible to anyone who knows the correct passphrase.
52+
Trang web giờ có thể truy cập cho bất kỳ ai biết passphrase đúng.
5353

54-
## Keeping secrets
54+
## Giữ bí mật
5555

56-
It's important that sensitive data doesn't accidentally end up being sent to the browser, where it could easily be stolen by hackers and scoundrels.
56+
Điều quan trọng là dữ liệu nhạy cảm không vô tình bị gửi tới trình duyệt, nơi nó có thể dễ dàng bị đánh cắp bởi hacker và những kẻ gian.
5757

58-
SvelteKit makes it easy to prevent this from happening. Notice what happens if we try to import `PASSPHRASE` into `src/routes/+page.svelte`:
58+
SvelteKit giúp bạn dễ dàng ngăn chặn điều này xảy ra. Lưu ý điều gì sẽ xảy ra nếu chúng ta thử import `PASSPHRASE` vào `src/routes/+page.svelte`:
5959

6060
```svelte
6161
/// file: src/routes/+page.svelte
@@ -65,28 +65,28 @@ SvelteKit makes it easy to prevent this from happening. Notice what happens if w
6565
</script>
6666
```
6767

68-
An error overlay pops up, telling us that `$env/static/private` cannot be imported into client-side code. It can only be imported into server modules:
68+
Một bảng lỗi xuất hiện, thông báo cho chúng ta rằng `$env/static/private` không thể được import vào mã chạy ở phía client. Nó chỉ có thể được import vào các mô-đun server:
6969

7070
- `+page.server.js`
7171
- `+layout.server.js`
7272
- `+server.js`
73-
- any modules ending with `.server.js`
74-
- any modules inside `src/lib/server`
73+
- bất kỳ mô-đun nào kết thúc bằng `.server.js`
74+
- bất kỳ mô-đun nào trong `src/lib/server`
7575

76-
In turn, these modules can only be imported by _other_ server modules.
76+
Lần lượt, những mô-đun này chỉ có thể được import bởi những mô-đun server _khác_.
7777

78-
## Static vs dynamic
78+
## Tĩnh và động
7979

80-
The `static` in `$env/static/private` indicates that these values are known at build time, and can be _statically replaced_. This enables useful optimisations:
80+
Chữ `static` trong `$env/static/private` chỉ ra rằng những giá trị này được biết tại thời điểm build và có thể được _thay thế tĩnh_. Điều này giúp ích trong việc tối ưu hóa:
8181

8282
```js
8383
/// no-file
8484
import { FEATURE_FLAG_X } from '$env/static/private';
8585

8686
if (FEATURE_FLAG_X === 'enabled') {
87-
// code in here will be removed from the build output
88-
// if FEATURE_FLAG_X is not enabled
87+
// mã ở đây sẽ bị xóa khỏi kết quả build
88+
// nếu FEATURE_FLAG_X không bằng enabled
8989
}
9090
```
9191

92-
In some cases you might need to refer to environment variables that are _dynamic_ — in other words, not known until we run the app. We'll cover this case in the next exercise.
92+
Trong một số trường hợp, bạn có thể cần tham chiếu đến các biến môi trường là _động_ - nói cách khác, ta không biết cho đến khi chúng ta chạy ứng dụng. Chúng ta sẽ đề cập đến trường hợp này trong bài tập tiếp theo.

content/tutorial/04-advanced-sveltekit/06-environment-variables/01-env-static-private/app-a/src/routes/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
<form method="POST">
66
<label>
7-
enter the passphrase
7+
nhập passphrase
88
<input name="passphrase" autocomplete="off" />
99
</label>
1010
</form>
1111

1212
{#if form?.incorrect}
13-
<p class="error">wrong passphrase!</p>
13+
<p class="error">sai passphrase!</p>
1414
{/if}
1515

1616
<style>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<h1>welcome!</h1>
1+
<h1>xin chào!</h1>
22

33
<form method="POST">
4-
<button>log out</button>
4+
<button>đăng xuất</button>
55
</form>

content/tutorial/04-advanced-sveltekit/06-environment-variables/02-env-dynamic-private/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: $env/dynamic/private
33
---
44

5-
If you need to read the values of environment variables when the app runs, as opposed to when the app is built, you can use `$env/dynamic/private` instead of `$env/static/private`:
5+
Nếu bạn cần đọc giá trị của biến môi trường khi ứng dụng chạy, thay vì khi ứng dụng được xây dựng, bạn có thể sử dụng `$env/dynamic/private` thay vì `$env/static/private`:
66

77
```js
88
/// file: src/routes/+page.server.js

content/tutorial/04-advanced-sveltekit/06-environment-variables/03-env-static-public/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
title: $env/static/public
33
---
44

5-
Some environment variables _can_ be safely exposed to the browser. These are distinguished from private environment variables with a `PUBLIC_` prefix.
5+
Một số biến môi trường _có thể_ được mở rộng an toàn cho trình duyệt. Chúng được phân biệt với biến môi trường riêng tư thông qua tiền tố `PUBLIC_`.
66

7-
Add values to the two public environment variables in `.env`:
7+
Thêm giá trị cho hai biến môi trường công khai trong `.env`:
88

99
```env
1010
/// file: .env
1111
PUBLIC_THEME_BACKGROUND=+++"steelblue"+++
1212
PUBLIC_THEME_FOREGROUND=+++"bisque"+++
1313
```
1414

15-
Then, import them into `src/routes/+page.svelte`:
15+
Sau đó, import chúng vào `src/routes/+page.svelte`:
1616

1717
```svelte
1818
/// file: src/routes/+page.svelte

content/tutorial/04-advanced-sveltekit/06-environment-variables/04-env-dynamic-public/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: $env/dynamic/public
33
---
44

5-
As with [private environment variables](/tutorial/env-static-private), it's preferable to use static values if possible, but if necessary we can use dynamic values instead:
5+
Tương tự như [biến môi trường riêng tư](/tutorial/env-static-private), nếu có thể, thì nên sử dụng giá trị tĩnh, nhưng nếu cần thiết, chúng ta cũng có thể sử dụng giá trị động:
66

77
```svelte
88
/// file: src/routes/+page.svelte
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"title": "Environment variables"
2+
"title": "Biến môi trường"
33
}

0 commit comments

Comments
 (0)