Skip to content

Translate 03-svelte-files.md from en to jp #13

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

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Changes from all commits
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
@@ -1,41 +1,41 @@
---
title: .svelte files
title: .svelte ファイル
Copy link
Author

Choose a reason for hiding this comment

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

.svelte files か .svelte ファイル かで迷いましたが後者にしました。
.svelte files の方が良さそうであれば教えてください

---

Components are the building blocks of Svelte applications. They are written into `.svelte` files, using a superset of HTML.
コンポーネントは Svelte アプリケーションの構成要素です。これらは `.svelte` ファイルに書かれ、HTML のスーパーセットを使って記述されます。
Copy link
Author

Choose a reason for hiding this comment

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

適切なスーパーセットの訳が思いつかなかったのでカタカナにしてます。


All three sections — script, styles and markup — are optional.
以下の3つのセクション — script, styles, markup — は任意です。

<!-- prettier-ignore -->
```svelte
/// file: MyComponent.svelte
<script module>
// module-level logic goes here
// (you will rarely use this)
// モジュールレベルのロジックはここに書きます
// (ほとんど使うことはありません)
</script>

<script>
// instance-level logic goes here
// インスタンスレベルのロジックはここに書きます
</script>

<!-- markup (zero or more items) goes here -->
<!-- マークアップ (0行以上) はここに書きます -->
Copy link
Author

Choose a reason for hiding this comment

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

zero or more items が行を示してるのかと思い日本語では行と訳してます


<style>
/* styles go here */
/* スタイルはここに書きます */
</style>
```

## `<script>`
Copy link
Author

Choose a reason for hiding this comment

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

英字のままの場合は ## 始める前に <!--Before-we-begin--> のような見出しアンカーにする必要はないと認識してますが、間違ってたら教えてください。


A `<script>` block contains JavaScript (or TypeScript, when adding the `lang="ts"` attribute) that runs when a component instance is created. Variables declared (or imported) at the top level can be referenced in the component's markup.
`<script>` ブロックは、コンポーネントインスタンスが作成されたときに実行される JavaScript (または `lang="ts` 属性を追加した時の TypeScript) を含みます。トップレベルで宣言 (またはインポート) された変数は、コンポーネントのマークアップで参照できます。

In addition to normal JavaScript, you can use _runes_ to declare [component props]($props) and add reactivity to your component. Runes are covered in the next section.
通常のJavaScriptに加えて、_runes_ を使用して[コンポーネントのプロパティ]($props)を宣言したり、コンポーネントにリアクティビティを追加したりできます。Runes については次のセクションで説明します。

<!-- TODO describe behaviour of `export` -->

## `<script module>`

A `<script>` tag with a `module` attribute runs once when the module first evaluates, rather than for each component instance. Variables declared in this block can be referenced elsewhere in the component, but not vice versa.
`<script>` タグに `module` 属性を付けると、モジュールが最初に評価される際に1回だけ実行され、各コンポーネントインスタンスごとに実行されるわけではありません。このブロックで宣言された変数は、コンポーネント内の他の場所から参照できますが、その逆はできません。

```svelte
<script module>
Expand All @@ -48,24 +48,24 @@ A `<script>` tag with a `module` attribute runs once when the module first evalu
</script>
```

You can `export` bindings from this block, and they will become exports of the compiled module. You cannot `export default`, since the default export is the component itself.
このブロックから `export` バインディングを使用してエクスポートすることができ、それらはコンパイルされたモジュールのエクスポートとして扱われます。ただし、コンポーネント自体にデフォルトエクスポートがされるため `export default` は使用できません。

> [!NOTE] If you are using TypeScript and import such exports from a `module` block into a `.ts` file, make sure to have your editor setup so that TypeScript knows about them. This is the case for our VS Code extension and the IntelliJ plugin, but in other cases you might need to setup our [TypeScript editor plugin](https://www.npmjs.com/package/typescript-svelte-plugin).
> [!NOTE] TypeScriptを使用していて、`module` ブロックからエクスポートされたものを `.ts` ファイルにインポートする場合は、TypeScriptがそれらを認識できるようにエディタの設定を行う必要があります。VS Code拡張機能やIntelliJプラグインを使用している場合はこの設定が必要となりますが、それ以外の場合は [TypeScript エディタープラグイン](https://www.npmjs.com/package/typescript-svelte-plugin) を設定する必要がある場合があります。

> [!LEGACY]
> In Svelte 4, this script tag was created using `<script context="module">`
> Svelte 4 では、このスクリプトタグは `<script context="module">` を使用して作成されました。

## `<style>`

CSS inside a `<style>` block will be scoped to that component.
`<style>` ブロック内のCSSは、そのコンポーネントにスコープされます。

```svelte
<style>
p {
/* this will only affect <p> elements in this component */
/* これはこのコンポネント内の <p> 要素にのみ影響します */
color: burlywood;
}
</style>
```

For more information, head to the section on [styling](scoped-styles).
詳細については、[スタイリング](scoped-styles) のセクションをご覧ください。
Loading