@@ -13,14 +13,12 @@ use crate::introspection::{Introspector, Locatable, Location};
1313use crate :: routines:: Routines ;
1414use crate :: World ;
1515
16- /// Manages stateful parts of your document.
16+ /// 文書中の状態の管理。
1717///
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._ というエラーメッセージを出力することが分かります。
2422///
2523/// ```typ
2624/// // This doesn't work!
@@ -38,17 +36,18 @@ use crate::World;
3836/// #compute("x - 5")
3937/// ```
4038///
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+ /// 今回の単純な例ではこの条件が満たされますが、一般的には必ずしもそうとは限りません。
4744///
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+ /// 以下の例を考えます。
5251///
5352/// ```example
5453/// #set heading(numbering: "1.")
@@ -64,28 +63,26 @@ use crate::World;
6463/// ...
6564/// ```
6665///
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`となります。
7170///
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`です。
7776///
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)が利用可能な場合にのみ使用できる _コンテキスト_ 関数です。
8279///
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+ /// 関数が与えられた場合、その関数は前の状態を受け取り、新しい状態を返さなければなりません。
8784///
88- /// Our initial example would now look like this:
85+ /// 最初の例は以下のようになります。
8986///
9087/// ```example
9188/// #let s = state("x", 0)
@@ -102,12 +99,10 @@ use crate::World;
10299/// #compute("x - 5")
103100/// ```
104101///
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`メソッドはコンテンツを返し、その影響は文書に返されたコンテンツが挿入された場所で生じます。
108104///
109- /// As a result, we can now also store some of the computations in variables,
110- /// but they still show the correct results:
105+ /// このようにして、計算結果を変数に保存できるようになり、正しい結果を表示しています。
111106///
112107/// ```example
113108/// >>> #let s = state("x", 0)
@@ -129,16 +124,14 @@ use crate::World;
129124/// #more
130125/// ```
131126///
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+ /// 良い例は見出しカウンターです。
129+ /// これはTypstの[カウンターシステム ]($counter)が状態システムにとてもよく似ているためです。
135130///
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.
131+ /// # タイムトラベル
132+ /// Typstの状態管理システムを使用するとタイムトラベルもできます!
133+ /// 文書内の任意の位置でその状態がどの値になっているのかを、どこからでも突き止めることができます。
134+ /// 特に、`at`メソッドを用いると特定の任意の位置での状態値が取得でき、`final`メソッドを用いると文書の終わりでの状態値を取得できます。
142135///
143136/// ```example
144137/// >>> #let s = state("x", 0)
@@ -160,17 +153,15 @@ use crate::World;
160153/// #compute("x - 5")
161154/// ```
162155///
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.
156+ /// # 注意事項 { #caution }
157+ /// 全ての状態値を解決するために、Typstはコードを複数回評価します。
158+ /// しかしながら、実際に状態操作が完全に解決されるかは保証されません。
167159///
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.
160+ /// 例えば、状態の最終的な値に依存する更新を行う状態を作成した場合、決して収束しなくなるでしょう。
161+ /// 以下の例はこの実演です。
162+ /// 状態を`1`で初期化し、続いて自身の最終値に1を足した値に更新します。
163+ /// したがって値は`2`になるべきですが、最終値が`2`となったので`3`に更新します。以下同様です。
164+ /// この例では有限値が表示されていますが、これは単にTypstが数回試行した後に諦めるためです。
174165///
175166/// ```example
176167/// // This is bad!
@@ -179,11 +170,9 @@ use crate::World;
179170/// #context s.get()
180171/// ```
181172///
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.
173+ /// 一般に、コンテキスト内部で更新を行う状態を作成しないようにしてください。
174+ /// 可能であれば、更新内容をコンテキストに依存しない値として、あるいは前の値から新しい値を計算する関数として定義してください。
175+ /// どうしても避けられない場合がありますが、その場合は結果が適切に収束することを保証することはあなたの責任です。
187176#[ ty( scope) ]
188177#[ derive( Debug , Clone , PartialEq , Hash ) ]
189178pub struct State {
@@ -270,21 +259,21 @@ impl State {
270259
271260#[ scope]
272261impl State {
273- /// Create a new state identified by a key.
262+ /// キーで識別される新しい状態の作成。
274263 #[ func( constructor) ]
275264 pub fn construct (
276- /// The key that identifies this state.
265+ /// 状態を識別するキー。
277266 key : Str ,
278- /// The initial value of the state.
267+ /// 状態の初期値。
279268 #[ default]
280269 init : Value ,
281270 ) -> State {
282271 Self :: new ( key, init)
283272 }
284273
285- /// Retrieves the value of the state at the current location.
274+ /// 現在のロケーションでの状態値を取得。
286275 ///
287- /// This is equivalent to `{state.at(here())}`.
276+ /// これは `{state.at(here())}`と等価です。
288277 #[ typst_macros:: time( name = "state.get" , span = span) ]
289278 #[ func( contextual) ]
290279 pub fn get (
@@ -297,26 +286,25 @@ impl State {
297286 self . at_loc ( engine, loc)
298287 }
299288
300- /// Retrieves the value of the state at the given selector's unique match.
289+ /// 指定したセレクターで一意に特定される対象の状態値を取得。
301290 ///
302- /// The `selector` must match exactly one element in the document. The most
303- /// useful kinds of selectors for this are [labels]($label) and
304- /// [locations]($location).
291+ /// `selector`は文書中で厳密に1つだけの要素にマッチしなければなりません。
292+ /// この目的で最も便利なセレクターは[ラベル]($label)と[ロケーション]($location)です。
305293 #[ typst_macros:: time( name = "state.at" , span = span) ]
306294 #[ func( contextual) ]
307295 pub fn at (
308296 & self ,
309297 engine : & mut Engine ,
310298 context : Tracked < Context > ,
311299 span : Span ,
312- /// The place at which the state's value should be retrieved.
300+ /// 状態値を取得する場所。
313301 selector : LocatableSelector ,
314302 ) -> SourceResult < Value > {
315303 let loc = selector. resolve_unique ( engine. introspector , context) . at ( span) ?;
316304 self . at_loc ( engine, loc)
317305 }
318306
319- /// Retrieves the value of the state at the end of the document.
307+ /// 文書の終わりでの状態値の取得。
320308 #[ func( contextual) ]
321309 pub fn final_ (
322310 & self ,
@@ -329,21 +317,18 @@ impl State {
329317 Ok ( sequence. last ( ) . unwrap ( ) . clone ( ) )
330318 }
331319
332- /// Update the value of the state.
320+ /// 状態値を更新。
333321 ///
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+ /// 更新は、返り値であるコンテンツが文書中に挿入された位置で適用されます。
323+ /// 文書中に出力がなければ何も起こりません!
324+ /// 例えば`{let _ = state("key").update(7)}`と書いた場合が、この何も起きないときに該当します。
325+ /// 状態の更新は常にレイアウト順に適用されるため、この場合にはTypstはいつ状態を更新するのか分かりません。
340326 #[ func]
341327 pub fn update (
342328 self ,
343329 span : Span ,
344- /// If given a non function-value, sets the state to that value. If
345- /// given a function, that function receives the previous state and has
346- /// to return the new state.
330+ /// 関数ではない値が与えられた場合、状態にその値を設定します。
331+ /// 関数が与えられた場合、その関数は前の状態を受け取り、新しい状態を返さなければなりません。
347332 update : StateUpdate ,
348333 ) -> Content {
349334 StateUpdateElem :: new ( self . key , update) . pack ( ) . spanned ( span)
0 commit comments