@@ -13,14 +13,12 @@ use crate::introspection::{Introspector, Locatable, Location};
13
13
use crate :: routines:: Routines ;
14
14
use crate :: World ;
15
15
16
- /// Manages stateful parts of your document.
16
+ /// 文書中で状態を持つ部分の管理。
17
17
///
18
- /// Let's say you have some computations in your document and want to remember
19
- /// the result of your last computation to use it in the next one. You might try
20
- /// something similar to the code below and expect it to output 10, 13, 26, and
21
- /// 21. However this **does not work** in Typst. If you test this code, you will
22
- /// see that Typst complains with the following error message: _Variables from
23
- /// outside the function are read-only and cannot be modified._
18
+ /// 文書中で何回か計算を行い、最後の計算結果を次の計算で使用するために記憶しておきたいとします。
19
+ /// 以下と同等のコードを試すと10、13、26、21と出力されることを期待するでしょう。
20
+ /// しかしTypstでは**そうはなりません**。
21
+ /// このコードを試してみると、Typstは _Variables from outside the function are read-only and cannot be modified._というエラーメッセージを出力することが分かります。
24
22
///
25
23
/// ```typ
26
24
/// // This doesn't work!
@@ -38,17 +36,18 @@ use crate::World;
38
36
/// #compute("x - 5")
39
37
/// ```
40
38
///
41
- /// # State and document markup { #state-and-markup }
42
- /// Why does it do that? Because, in general, this kind of computation with side
43
- /// effects is problematic in document markup and Typst is upfront about that.
44
- /// For the results to make sense, the computation must proceed in the same
45
- /// order in which the results will be laid out in the document. In our simple
46
- /// example, that's the case, but in general it might not be.
39
+ /// # 状態と文書のマークアップ { #state-and-markup }
40
+ /// なぜこうなるのでしょうか?
41
+ /// 一般的に副作用を伴うこの手の計算は文書のマークアップにおいて問題を引き起こすためで、Typstではこれをエラーとして扱います。
42
+ /// この結果を理解するには、計算処理が文書内で生成物がレイアウトされる順序と同じ順序で行われる必要があります。
43
+ /// 今回の単純な例ではこの条件が満たされますが、一般的には必ずしもそうとは限りません。
47
44
///
48
- /// Let's look at a slightly different, but similar kind of state: The heading
49
- /// numbering. We want to increase the heading counter at each heading. Easy
50
- /// enough, right? Just add one. Well, it's not that simple. Consider the
51
- /// following example:
45
+ /// 見出しの番号付けという、類似した状態ですが、少し異なる例を見てみましょう。
46
+ /// 各見出しで見出しカウンターの値を増やしたいとします。
47
+ /// 簡単ですよね?
48
+ /// ただ1を足すだけです。
49
+ /// 残念ながらそう単純ではないのです。
50
+ /// 以下の例を考えます。
52
51
///
53
52
/// ```example
54
53
/// #set heading(numbering: "1.")
@@ -64,28 +63,26 @@ use crate::World;
64
63
/// ...
65
64
/// ```
66
65
///
67
- /// Here, Typst first processes the body of the document after the show rule,
68
- /// sees the `Introduction` heading, then passes the resulting content to the
69
- /// `template` function and only then sees the ` Outline`. Just counting up would
70
- /// number the `Introduction` with `1` and the `Outline` with `2`.
66
+ /// ここで、Typstはまずshowルール以降の文書本体を処理し、`Introduction`見出しを検知します。
67
+ /// 続いて`template`関数に生成コンテンツを渡します。
68
+ /// その後、初めて` Outline`を検知します。
69
+ /// 単にカウンター値を増やすと `Introduction`は `1`に、 `Outline`は `2`となります。
71
70
///
72
- /// # Managing state in Typst { #state-in-typst }
73
- /// So what do we do instead? We use Typst's state management system. Calling
74
- /// the `state` function with an identifying string key and an optional initial
75
- /// value gives you a state value which exposes a few functions. The two most
76
- /// important ones are `get` and `update`:
71
+ /// # Typstにおける状態管理 { #state-in-typst }
72
+ /// それでは代わりにどうするのでしょうか?
73
+ /// Typstの状態管理システムを使用します。
74
+ /// 識別用のキーとなる文字列とオプションの初期値とともに` state`関数を呼び出すことで状態の値が得られます。
75
+ /// この状態の値はいくつかの関数を公開しており、最も重要な2つの関数が `get`と `update`です。
77
76
///
78
- /// - The [`get`]($state.get) function retrieves the current value of the state.
79
- /// Because the value can vary over the course of the document, it is a
80
- /// _contextual_ function that can only be used when [context]($context) is
81
- /// available.
77
+ /// - [`get`]($state.get)関数は状態の現在値を取得します。
78
+ /// 値は文書中で変化するため、これは[コンテキスト]($context)が利用可能な場合にのみ使用できる _コンテキスト_ 関数です。
82
79
///
83
- /// - The [`update`]($state.update) function modifies the state. You can give it
84
- /// any value. If given a non-function value, it sets the state to that value.
85
- /// If given a function, that function receives the previous state and has to
86
- /// return the new state.
80
+ /// - [`update`]($state.update)関数は状態に修正を加えます。
81
+ /// 任意の値が使用できます。
82
+ /// 関数ではない値が渡された場合、状態にその値が設定されます。
83
+ /// 関数が与えられた場合、その関数は前の状態を受け取り、新しい状態を返さなければなりません。
87
84
///
88
- /// Our initial example would now look like this:
85
+ /// 最初の例は以下のようになります。
89
86
///
90
87
/// ```example
91
88
/// #let s = state("x", 0)
@@ -102,12 +99,10 @@ use crate::World;
102
99
/// #compute("x - 5")
103
100
/// ```
104
101
///
105
- /// State managed by Typst is always updated in layout order, not in evaluation
106
- /// order. The `update` method returns content and its effect occurs at the
107
- /// position where the returned content is inserted into the document.
102
+ /// Typstが管理する状態は常に評価順ではなくレイアウト順に更新されます。
103
+ /// `update`メソッドはコンテンツを返し、その影響は文書に返されたコンテンツが挿入された場所で生じます。
108
104
///
109
- /// As a result, we can now also store some of the computations in variables,
110
- /// but they still show the correct results:
105
+ /// このようにして、計算結果を変数に保存できるようになりましたが、正しい結果を表示しています。
111
106
///
112
107
/// ```example
113
108
/// >>> #let s = state("x", 0)
@@ -129,16 +124,13 @@ use crate::World;
129
124
/// #more
130
125
/// ```
131
126
///
132
- /// This example is of course a bit silly, but in practice this is often exactly
133
- /// what you want! A good example are heading counters, which is why Typst's
134
- /// [counting system]($counter) is very similar to its state system.
127
+ /// この例はもちろん少々たわいないですが、これが実際に本当に必要となることがよくあります!
128
+ /// Typstの[カウンターシステム]($counter)は状態システムととてもよく似ているため、見出しカウンターが良い例です。
135
129
///
136
- /// # Time Travel
137
- /// By using Typst's state management system you also get time travel
138
- /// capabilities! We can find out what the value of the state will be at any
139
- /// position in the document from anywhere else. In particular, the `at` method
140
- /// gives us the value of the state at any particular location and the `final`
141
- /// methods gives us the value of the state at the end of the document.
130
+ /// # タイムトラベル
131
+ /// Typstの状態管理システムを使用するとタイムトラベルもできます!
132
+ /// We can find out what the value of the state will be at any position in the document from anywhere else.
133
+ /// 特に、`at`メソッドを用いると特定の任意の位置での状態の値が取得でき、`final`メソッドを用いると文書の終わりでの状態の値を取得できます。
142
134
///
143
135
/// ```example
144
136
/// >>> #let s = state("x", 0)
@@ -160,17 +152,15 @@ use crate::World;
160
152
/// #compute("x - 5")
161
153
/// ```
162
154
///
163
- /// # A word of caution { #caution }
164
- /// To resolve the values of all states, Typst evaluates parts of your code
165
- /// multiple times. However, there is no guarantee that your state manipulation
166
- /// can actually be completely resolved.
155
+ /// # 注意事項 { #caution }
156
+ /// 全ての状態の値を解決するために、Typstはコードを複数回評価します。
157
+ /// However, there is no guarantee that your state manipulation can actually be completely resolved.
167
158
///
168
- /// For instance, if you generate state updates depending on the final value of
169
- /// a state, the results might never converge. The example below illustrates
170
- /// this. We initialize our state with `1` and then update it to its own final
171
- /// value plus 1. So it should be `2`, but then its final value is `2`, so it
172
- /// should be `3`, and so on. This example displays a finite value because Typst
173
- /// simply gives up after a few attempts.
159
+ /// 例えば、 if you generate state updates depending on the final value of a state, the results might never converge.
160
+ /// The example below illustrates this.
161
+ /// We initialize our state with `1` and then update it to its own final value plus 1.
162
+ /// So it should be `2`, but then its final value is `2`, so it should be `3`, and so on.
163
+ /// This example displays a finite value because Typst simply gives up after a few attempts.
174
164
///
175
165
/// ```example
176
166
/// // This is bad!
@@ -179,11 +169,9 @@ use crate::World;
179
169
/// #context s.get()
180
170
/// ```
181
171
///
182
- /// In general, you should try not to generate state updates from within context
183
- /// expressions. If possible, try to express your updates as non-contextual
184
- /// values or functions that compute the new value from the previous value.
185
- /// Sometimes, it cannot be helped, but in those cases it is up to you to ensure
186
- /// that the result converges.
172
+ /// 一般に, you should try not to generate state updates from within context expressions.
173
+ /// 可能なら try to express your updates as non-contextual values or functions that compute the new value from the previous value.
174
+ /// Sometimes, it cannot be helped, but in those cases it is up to you to ensure that the result converges.結果を収束させるのはあなたの責任です。
187
175
#[ ty( scope) ]
188
176
#[ derive( Debug , Clone , PartialEq , Hash ) ]
189
177
pub struct State {
@@ -329,14 +317,12 @@ impl State {
329
317
Ok ( sequence. last ( ) . unwrap ( ) . clone ( ) )
330
318
}
331
319
332
- /// Update the value of the state.
320
+ /// 状態の値を更新。
333
321
///
334
- /// The update will be in effect at the position where the returned content
335
- /// is inserted into the document. If you don't put the output into the
336
- /// document, nothing happens! This would be the case, for example, if you
337
- /// write `{let _ = state("key").update(7)}`. State updates are always
338
- /// applied in layout order and in that case, Typst wouldn't know when to
339
- /// update the state.
322
+ /// The update will be in effect at the position where the returned content is inserted into the document.
323
+ /// If you don't put the output into the document, nothing happens!
324
+ /// This would be the case, for example, if you write `{let _ = state("key").update(7)}`.
325
+ /// State updates are always applied in layout order and in that case, Typst wouldn't know when to update the state.
340
326
#[ func]
341
327
pub fn update (
342
328
self ,
0 commit comments