Documentation ¶
Overview ¶
Package engine は、各翻訳エンジンの基本となる構造体とそのメソッドを定義しています.
Index ¶
- Constants
- type AccountInfo
- type Properties
- func (p *Properties) GetAPIKey() string
- func (p *Properties) GetQuotaLeft() (int, error)
- func (p *Properties) SetAPIKey(apiKey string) func()
- func (p *Properties) SetDefault()
- func (p *Properties) SetFuncGetInfoAPI(getInfoFunc func(properties *Properties) (AccountInfo, error))
- func (p *Properties) SetFuncTrans(...)
- func (p *Properties) Translate(inTxt string, langFrom string, langTo string) (outText string, isCache bool, err error)
- func (p *Properties) UniformLang(lang string) string
Constants ¶
const NameVarEnvAPIKeyDefault = "QIITRANS_API_KEY"
NameVarEnvAPIKeyDefault は環境変数の変数名で、翻訳 API のアクセストークンを設定するための変数名です.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AccountInfo ¶
type AccountInfo struct {
CharacterLeft int // = 利用可能文字数 - 利用済み文字数
}
AccountInfo は、利用中の翻訳 API の必要最低限の情報を保持する構造体です.
type Properties ¶
type Properties struct { // 翻訳済みのテキストをキャッシュするためのオブジェクトのポインタです Cache *cache.TCache // NameEngine は利用している翻訳エンジン名です. NameEngine string // 環境変数からアクセス・トークンを取得する際の環境変数名 NameVarEnvAPIKey string // 1 リクエストごとに sleep させる秒数です(デフォルト: 1) TimeInterval int // アクセス・トークンが無料アカウントの場合は true、有料アカウントの場合は false にセットします。(デフォルト: true) IsAccountFree bool // true の場合はキャッシュを更新します。false の場合はなるべくキャッシュを使います(デフォルト: false) Update bool // contains filtered or unexported fields }
Properties は各翻訳エンジンの基本となる構造体です. 翻訳エンジンが API を呼び出すのに必要な基本情報と、翻訳に使われるメソッド(クラス関数)を持った構造体です.
func (*Properties) GetAPIKey ¶
func (p *Properties) GetAPIKey() string
GetAPIKey はコマンドのオプションで指定されたアクセス・トークンを返します. オプションで指定がされていない場合は、環境変数から読み取って返します.
func (*Properties) GetQuotaLeft ¶
func (p *Properties) GetQuotaLeft() (int, error)
GetQuotaLeft は API のリクエスト残量(翻訳可能文字数)を返します。有料アカウントなど、制限がない場合は -1 を返します.
func (*Properties) SetAPIKey ¶
func (p *Properties) SetAPIKey(apiKey string) func()
SetAPIKey はコマンド引数から取得したアクセストークン/認証キー("apiKey")を翻訳エンジンが使えるようにセットします.
このメソッドは呼び出し元の defer 用に関数を返します。各々の翻訳エンジンが参照する環境変数に apiKey の値をセットするため、 既存の値があった場合は処理後 defer で元に戻せるようにするための関数です.
myEngine := deepleng.New("myCacheID") myAPIKey := "foobar" defer myEngine.SetAPIKey(myAPIKey)
func (*Properties) SetDefault ¶
func (p *Properties) SetDefault()
SetDefault はプロパティ(オブジェクトのフィールド)値を初期値に設定します.
func (*Properties) SetFuncGetInfoAPI ¶
func (p *Properties) SetFuncGetInfoAPI(getInfoFunc func(properties *Properties) (AccountInfo, error))
SetFuncGetInfoAPI メソッドは翻訳用の関数を割り当てます.
func (*Properties) SetFuncTrans ¶
func (p *Properties) SetFuncTrans(transFunc func( properties *Properties, inputText string, langFrom string, langTo string, ) (string, error))
SetFuncTrans メソッドは翻訳用の関数を割り当てます.
func (*Properties) Translate ¶
func (p *Properties) Translate(inTxt string, langFrom string, langTo string) (outText string, isCache bool, err error)
Translate は翻訳エンジンから割り当てられた翻訳関数を実行し、inText を langFrom から langTo に翻訳した結果を返します. この値はキャッシュされます。キャッシュを更新したい場合は Update フィールド(プロパティ)を true にセットしてください.
func (*Properties) UniformLang ¶
func (p *Properties) UniformLang(lang string) string
UniformLang は lang を API が検知可能な書式に変換します. もし lang が未対応の言語だった場合は、空の値を返します.