# react-native: GT JSXデータ形式 URL: https://generaltranslation.com/ja/docs/react-native/reference/gt-jsx.mdx --- title: GT JSXデータ形式 description: General Translation のミニファイされた JSXデータ形式のリファレンス --- {/* 自動生成: 直接編集しないでください。代わりに content/docs-templates/ 内のテンプレートを編集してください。 */} GT JSXデータ形式は、Reactアプリケーション内の翻訳済みUIを表現するために、General Translationライブラリで使用されるミニファイされたデータ形式です. ## はじめに: JSX ツリー React では、JSX ツリーは次のような構造を持つオブジェクトとして表現されます。 ```ts type Element = { type: string; props: { children: JSXTree[] | JSXTree; // ...その他のprops }; // ...その他の属性 }; type JSXTree = Element | string; ``` GT JSX は、この JSX ツリー構造を圧縮した表現で、React アプリケーション内の翻訳済み UI を表すために General Translation ライブラリで使用されます。 ## リファレンス ```ts type Element = { t?: string; // tag name c?: (Element | Variable | string)[]; // 子要素 i?: number; // GT ID of the element d?: { b?: Record; // 分岐 t?: "p" | "b"; // branch transformation type (plural or branch) pl?: string; // placeholder ti?: string; // title alt?: string; // alt arl?: string; // aria-label arb?: string; // aria-labelledby ard?: string; // aria-describedby s?: Record; // スタイル }; } type Variable = { k: string; // key v?: "v" | "n" | "c" | "d" | "rt"; // 型 i?: number; // GT ID } type GTJSXTree = Element | Variable | string | (Element | Variable | string)[]; ``` ## GT JSX: 文字列 GT JSX の最もシンプルな形式は文字列で、静的なテキストを表します。 たとえば: ```jsx Hello, world! ``` GT JSX では次のように表現されます: ```ts "Hello, world!" ``` 文字列の配列も GT JSX として有効です: ```ts ["Hello, ", "world!"] ``` ## GT JSX: 要素 GT では、JSX の `Element` 型を 2 通りの方法で表現します。 ### 変数 1つ目は変数で、キーと省略可能な型を持つシンプルなオブジェクトです。これは、runtime で変化しうる変数を表すために使用されます。 ```ts type Variable = { k: string; // `k` represents key, the name of the variable v?: ( // represents the type of the variable, if left out is assumed as `v` "v" | // `v`, a generic variable "n" | // `n`, a number variable "c" | // `c`, a currency variable "d" | // `d`, a datetime variable "rt" // `rt`、relative time 変数 ); i?: number; // GT ID of the variable } ``` #### 例 1: Var ```jsx Hello, {name}! ``` GT JSX では、次のように表現されます。 ```ts ["Hello, ", { k: "_gt_var_1", i: 1 }, "!"] ``` `name` propを持たない変数には、GT IDに基づく一意の内部名が割り当てられます #### 例 2: Num ```jsx The count is {count} ``` GT JSX では次のように表現されます: ```ts ["The count is ", { k: "count", v: "n", i: 1 }] ``` #### 例 3: name prop を使う場合 ```jsx This product costs {amount} ``` GT JSX では次のように表現されます。 ```ts ["This product costs ", { k: "cost", v: "c", i: 1 }] ``` ### 要素 変数ではない要素は、次のデータ構造で表されます。 これらの属性はすべて省略可能です。 空のオブジェクトは、元の対応する要素と同じ位置にある翻訳済み要素であり、その子孫に翻訳可能な内容が含まれていないことを表します。 **実際には、`i` は常に含まれます。** ```ts type Element = { t?: string; // タグ名 c?: GTJSXTree | GTJSXTree[]; // 子要素 i?: number; // 要素のGT ID d?: { // data-_gt プロパティ b?: Record; // 分岐 t?: "p" | "b"; // 分岐変換タイプ(複数形または分岐) pl?: string; // プレースホルダー ti?: string; // タイトル alt?: string; // alt arl?: string; // aria-label arb?: string; // aria-labelledby ard?: string; // aria-describedby s?: Record; // スタイル } } ``` #### 例 1: シンプルなタグ ```jsx Hello, world! ``` GT JSX では次のように表現されます: ```ts ["Hello, ", { c: "world", i: 1 }, "!"] ``` #### 例 2: ネストあり、変数を使用 ```jsx Hello, my name is {name} ``` GT JSX では次のように表現できます: ```ts [ { t: "b", c: "Hello", i: 1 }, ", my name is ", { t: "i", c: { k: "_gt_var_3", i: 3 }, i: 2 } ] ``` #### 例 3: 複数形の場合 ```jsx I have {count} item} other={<>I have {count} items} /> ``` GT JSX では次のように表現されます: ```ts { i: 1, d: { t: "p", b: { one: { c: ["I have", { k: "_gt_num_4", v: "n", i: 3 }, "item"], i: 2 }, other: { c: ["I have", { k: "_gt_num_4", v: "n", i: 3 }, "items"], i: 2 // 分岐では同じIDが使用されることに注意 } } } } ``` ### GTJSX 型 ```ts type GTJSXTree = Element | Variable | string | (Element | Variable | string)[]; ``` ## GT ID GT ID は、JSX ツリー内の要素と変数に対して、1 から始まる深さ優先の連番で割り当てられます。 `` や `` のような分岐コンポーネントがある場合、並列な分岐には同じ GT ID が割り当てられます。これは、ある言語では別の言語より分岐数が多い場合 (たとえば、複数形の形式がより多い言語など) でも、適切な props とロジックを持つ要素を引き続き作成できるようにするためです。 ## GT JSX JSON ファイル 各 JSX Tree は、`` コンポーネントの子要素を表します。 コンポーネントは翻訳用の JSON ファイルにまとめて保存されます。 これらのファイルに保存される JSON オブジェクトの型は、次のとおりです。 ```ts type GTJSXFile = { [key: string]: GTJSXTree; } ``` ここで `key` はユーザー定義の値、または元の言語の `GTJSXTree` のハッシュ値であり、 `GTJSXTree` は上で説明した GT JSX ツリーの型です。 ### 完全な例 記述したJSX: ```jsx Alice's happy customer ``` 次のJSXツリーで表されます。 ```ts [ { type: "b", "props": { "children": "Alice's" } }, " happy ", { type: "i", "props": { "children": "customer" } } ] ``` これは次のような GT JSX にミニファイされます: ```ts [{ t: "b", c: "Alice's", i: 1 }, " happy ", { t: "i", c: "customer", i: 2 }] ``` スペイン語に翻訳した場合、GT JSX は次のようになります。 ```ts [{ c: "El cliente", i: 2 }, " feliz ", { c: "de Alice", i: 1 }] ``` 次のような内容のファイルに保存されます: ```json { "abc123": [{ "c": "El cliente", "i": 2 }, " feliz ", { "c": "de Alice", "i": 1 }] } ``` `abc123` はスペイン語訳ではなく、元の英語の GT JSX ツリーのハッシュです。 スペイン語訳を元の JSX ツリーと突き合わせると、次のようになります。 ```ts [ { type: "i", "props": { "children": "El cliente" } }, " feliz ", { type: "b", "props": { "children": "de Alice" } } ] ``` その後、意図したUIとして表示できます。 ```jsx <>El cliente feliz de Alice ``` ### ハッシュ ハッシュアルゴリズム、ハッシュ長は未定 ### runtime でのハッシュ計算を避ける runtime でハッシュを計算しなくて済むように、これらのライブラリには、ユーザーが ID を指定できるオプションのフォールバック機構が用意されています。 ```jsx Hello, world ``` 翻訳用のJSONファイルにIDがある場合は、ハッシュではなくそのIDが使用されます。 ```json { "example": "Hello, world" } ```