Skip to content

Translate language extensions of playground examples into japanese #230

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
Show file tree
Hide file tree
Changes from 4 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
@@ -0,0 +1,81 @@
// EnumsはTypeScriptを用いてJavaScriptで
// 固定値の集合を簡単に扱うための機能です。

// デフォルトでは、enumは0から始まり
Copy link
Contributor

Choose a reason for hiding this comment

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

Enumsenum の二種類の表記が使われているのが少し気になりました。
原文で、複数形・単数形の種別、冒頭の大文字有無があったからなのかなぁ、と思うのですが、こういうケースってどうするのが適切なんでしょう。。。

  1. enum で統一
  2. Enum で統一
  3. 列挙型 で統一

あたりなんでしょうか。。。他案件の翻訳でどうしてたか、とかあれば知りたいです。。。

Copy link
Contributor

@Naturalclar Naturalclar Feb 10, 2020

Choose a reason for hiding this comment

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

Gatsby の翻訳では基本的に以下のようなフォーマットでやっています。

  • 固有名詞(Wordpressなど)は英語、大文字。
  • コマンドの一部(gatsby deploy など)はコマンドのまま。
  • 一般的に英語で見る単語(完全にOpinionated ですが、Github の Issue など)は英語、大文字。
  • それ以外の単語は適切な訳、またはカタカナ英語

Enum は TypeScript の言語機能の一部であり、他の言語でも Enum としてよく見られる印象(imo)なので、

Enum で統一が良いかなと思いました。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

const enumだけそのままで残して、他はEnumに統一しました

// オプション1つにつき1ずつ増加していく数値です。
// これは値が重要でないときに便利です。

enum CompassDirection {
North,
East,
South,
West,
}

// enumオプションに注釈を付けると、値を設定できます。
// 注釈をつけなければ、値のインクリメントは設定された値を引き継いで行われます。

enum StatusCodes {
OK = 200,
BadRequest = 400,
Unauthorized,
PaymentRequired,
Forbidden,
NotFound,
}

// enumはEnumName.Valueで参照できます。
Copy link
Contributor

Choose a reason for hiding this comment

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

[imo] Enumname.Valueで伝わるかが悩ましい、そういった予約語のように見えてしまう気がします。

Suggested change
// enumはEnumName.Valueで参照できます
// 各EnumはEnum名.値名で参照できます


const startingDirection = CompassDirection.East
const currentStatus = StatusCodes.OK

// Enumsはキーから値と値からキーの双方による
// アクセスをサポートしています。

const okNumber = StatusCodes.OK
const okNumberIndex = StatusCodes['OK']
const stringBadRequest = StatusCodes[400]

// Enumsに異なる型を設定することもできます。文字列型が一般的です。
// 文字列型を用いると、runtimeで数字を探す必要がなくなるので、
Copy link
Contributor

Choose a reason for hiding this comment

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

これは、どこまで英字表記 / カタカナ語を使うか、という話で正解があるわけではないのですが、個人的には runtime実行時 と日本語にしてしまってもいいのかなーと思います。

// デバッグが簡単になります。

enum GamePadInput {
Up = 'UP',
Down = 'DOWN',
Left = 'LEFT',
Right = 'RIGHT',
}

// もし、JavaScript runtimeでobjectの数を減らしたいなら、
// const enumが使えます。

// const enumの値は
// runtimeでobjectを介して対応する値を見つけるのではなく、
// TypeScriptによるコードのトランスパイル時に置換されます。
Copy link
Contributor

Choose a reason for hiding this comment

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

ここも上の話と近いです。 トランスパイル をカタカナで扱うのであれば、やはり runtimeで実行時に と訳したほうがスッキリするかな、と。


const enum MouseAction {
MouseDown,
MouseUpOutside,
MouseUpInside,
}

const handleMouseAction = (action: MouseAction) => {
switch (action) {
case MouseAction.MouseDown:
console.log('Mouse Down')
break
}
}

// トランスパイルされたJavaScriptのコードを見ると、
// 他のenumsはobjectや関数として残っているのに、
// MouseActionだけが残っていないことに気がつくでしょう。

// これは、handleMouseActionのswitch文にある
// MouseAction.MouseDownについても同様です。

// Enumsには他にも多くの機能があります。
// 詳しくはTypeScriptハンドブックをご覧ください。
//
// https://www.typescriptlang.org/docs/handbook/enums.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 公称型システムではそれぞれの型がユニークであり、
// ある2つの型が同じデータ構造をしていても
// 型をまたいで代入することはできません

// TypeScriptの型システムは構造的です。
// 構造的型システムでは、ある型がアヒル型と同じ構造をしていたら、それをアヒル型として扱います。
// また、ガチョウ型がアヒル型とまったく同じ属性を持っていたら、ガチョウ型はアヒル型としても扱われます。
// より詳しいことはこちらをご覧ください: example:structural-typing

// これにはいくつかの欠点があります。
// 文字列や数値が特別なコンテキストを持っており、
// 他の値に変換可能にしたくない場合などが該当します。
// 例:
//
// - ユーザーの入力した安全でない文字列
// - 翻訳された文字列
// - ユーザー識別番号
// - アクセストークン

// 少しの余分なコードを足すだけで、
// 公称型システムと同じような恩恵に預かることができます。

// 交差型を用いて、__brand (この名前は慣習的なもの)という
// プロパティを持つ型を作成し、
// 通常の文字列が代入不可能な
// ValidatedInputStringという型を作ってみましょう。

type ValidatedInputString = string & { __brand: 'User Input Post Validation' }

// 文字列をValidatedInputString型に変換するために関数を使います。
// 注目に値するのは、validateUserInputを通過した文字列はValidatedInputStringだと
// TypeScriptに__伝えて__いる点です。
Copy link
Contributor

Choose a reason for hiding this comment

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

原文だと、 _telling_ なので、左右のアンダースコアは1ずつ?

Copy link
Contributor

Choose a reason for hiding this comment

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

React の翻訳の時、日本語の文字は斜体でも強調が伝わりづらいと言う理由から、あえて太字にしてたりしましたね。
ただ、これは Markdown ではないので原文通りアンダースコア1つずつで良さそう。


const validateUserInput = (input: string) => {
const simpleValidatedInput = input.replace(/\</g, '≤')
return simpleValidatedInput as ValidatedInputString
}

// 次に、普通の文字列型は受け取らず、
// 作成した公称型であるValidatedInputStringだけを受け取る関数を作ってみます。

const printName = (name: ValidatedInputString) => {
console.log(name)
}

// 例えば、ユーザーからの安全でない入力を受け取り、
// バリデーターに通してから出力してみます。

const input = "\n<script>alert('bobby tables')</script>"
const validatedInput = validateUserInput(input)
printName(validatedInput)

// 一方でバリデートしていない文字列をprintNameに渡すと、
// コンパイルエラーが発生します。

printName(input)

// 以下の400コメントがついたGitHubのissueに、
// 公称型を作成する色々な方法のまとめと
// それらのトレードオフがまとまっています:
//
// https://github.com/Microsoft/TypeScript/issues/202
//
// 他にも以下の記事が分かりやすいまとめになっています:
//
// https://michalzalecki.com/nominal-typing-in-typescript/
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// オブジェクトの形を宣言するのに使う
// 2つの主なツールがinterfacesとtype aliasです。
Copy link
Contributor

Choose a reason for hiding this comment

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

日本語では単数形で良さそう。

Suggested change
// 2つの主なツールがinterfacesとtype aliasです。
// 2つの主なツールがinterfaceとtype aliasです。

//
// これらはとても似ており、ほとんどの場合、
// 同じ振る舞いをします。

type BirdType = {
wings: 2
}

interface BirdInterface {
wings: 2
}

const bird1: BirdType = { wings: 2 }
const bird2: BirdInterface = { wings: 2 }

// TypeScriptは構造的型システムを採用しているので、
// どちらの使い方も混在させることができます。

const bird3: BirdInterface = bird1

// どちらも他のinterfacesやtypesからの拡張をサポートしています。
Copy link
Contributor

Choose a reason for hiding this comment

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

日本語では単数形で良さそう。

Suggested change
// どちらも他のinterfacesやtypesからの拡張をサポートしています
// どちらも他のinterfaceやtypeからの拡張をサポートしています

// type aliasesは交差型を、
Copy link
Contributor

Choose a reason for hiding this comment

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

ここも単数形と複数形の違いが少し気になりました。
(このファイルの冒頭では、 2つの主なツールがinterfacesとtype alias となっているので)

Copy link
Contributor

Choose a reason for hiding this comment

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

これも主観に左右される話なので、あくまでIMOとして書きます。極力日本語 or カタカナ語に置き換えてしまって良いと思っています。
インタフェース or インターフェース

FYI: 以下の書籍では インタフェース となっていました

https://www.amazon.co.jp/Java%E8%A8%80%E8%AA%9E%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E3%83%AC%E3%83%83%E3%82%B9%E3%83%B3-%E7%AC%AC3%E7%89%88-%E4%B8%8A-Java%E8%A8%80%E8%AA%9E%E3%82%92%E5%A7%8B%E3%82%81%E3%82%88%E3%81%86-%E7%B5%90%E5%9F%8E/dp/4797371250

Copy link
Contributor Author

Choose a reason for hiding this comment

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

コード中に使う言語機能に紐付いた単語は英語のままの方が読みやすいのでは?というIMOです
とはいえ、 @Quramy さんの言うことも分かるので、どっちかに統一してそっちに合わせたいところ

// interfacesはextendsキーワードを使用します。

type Owl = { nocturnal: true } & BirdType
type Robin = { nocturnal: false } & BirdInterface

interface Peacock extends BirdType {
colourful: true
flies: false
}
interface Chicken extends BirdInterface {
colourful: false
flies: false
}

let owl: Owl = { wings: 2, nocturnal: true }
let chicken: Chicken = { wings: 2, colourful: false, flies: false }

// そうは言っても、type aliasesよりもinterfacesを使うことをおすすめします。
// 具体的には、より良いエラーメッセージが得られるからです。
// 以下のエラーにマウスオーバーしてみると、
// Chickenのようなinterfacesを使ったときの方が簡潔でより分かりやすいメッセージが
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Chickenのようなinterfacesを使ったときの方が簡潔でより分かりやすいメッセージが
// Chickenのようなinterfaceを使ったときの方が簡潔でより分かりやすいメッセージが

// TypeScriptより示されているのが分かるでしょう。

owl = chicken
chicken = owl

// type aliasesとinterfacesの最も大きな違いの1つは、
// interfacesが開かれた型であるのに対して、type aliasesは閉じた型であることです。
// これは、interfaceは2回目の宣言で拡張が可能であることを
// 意味します。

interface Kitten {
purrs: boolean
}

interface Kitten {
colour: string
}

// 一方で、type aliaseは一度宣言した後に、
// 外からその型の内容を変更することはできません。

type Puppy = {
color: string
}

type Puppy = {
toys: number
}

// 達成したい目標に依って、この違いは利点にも欠点にもなり得ます。
// しかし、公開される型については
// interfaceを用いる方が良いでしょう。

// type aliasesとinterfacesにおけるすべてのエッジケースを確認するのに
// 最適な資料の1つである以下のstack overflowスレッドは
// 初めて調べるのにちょうど良いでしょう:

// https://stackoverflow.com/questions/37233735/typescript-interfaces-vs-types/52682220#52682220
55 changes: 55 additions & 0 deletions packages/playground-examples/copy/ja/sections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"sections": [
{
"name": "JavaScript",
"id": "JavaScript",
"subtitle": "See how TypeScript improves day to day working with JavaScript with minimal additional syntax."
},
{
"name": "TypeScript",
"id": "TypeScript",
"subtitle": "どのようにしてTypeScriptがJavaScriptを拡張して、より安全で便利にしているかを確認する。"
},
{
"name": "3.7",
"id": "3.7",
"subtitle": "See the <a href='https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/'>Release notes</a>."
},
{
"name": "3.8",
"id": "3.8",
"subtitle": "See the <a href='https://devblogs.microsoft.com/typescript/announcing-typescript-3-8-beta/'>Beta Release notes</a>."
},
{
"name": "Playground V3",
"id": "Playground",
"subtitle": "Learn what has changed in this website."
}
],

"sortedSubSections": [
// JS
"JavaScript Essentials",
"Functions with JavaScript",
"Working With Classes",
"Modern JavaScript",
"External APIs",
"Helping with JavaScript",
// TS
"Primitives",
"Type Primitives",
"Meta-Types",
"Language",
"Language Extensions",
// Examples
"Syntax and Messaging",
"Types and Code Flow",
"Fixits",
// Playground
"Config",
"Tooling",
// 3.8
"Breaking Changes",
"JSDoc Improvements"
]
}