2021/8/27 : Flutter : Guide for building internationalized Flutter apps の訳パート2

 

<<前のページ(パート1)へ


Contents

Defining and Using Simple Messages

A simple message has no parameters. The message’s translation is exposed by a String-valued get method on the generated AppLocalizations class. So, as noted earlier, a message named helloWorld is defined like this:

単純なメッセージにはパラメータがありません。 メッセージの翻訳は、生成されたAppLocalizationsクラスの文字列値のgetメソッドによって公開されます。 したがって、前述のように、helloWorldという名前のメッセージは次のように定義されます。

{
  "@@locale": "en",

  "helloWorld": "Hello World",
  "@helloWorld": {
    "description": "The conventional newborn programmer greeting"
  }
}

And used like this:

そして下記のように使用されます。

AppLocalizations.of(context).helloWorld

 

Translations can include special characters like newlines, if the usual JSON escapes are used:

通常のJSONエスケープが使用されている場合、翻訳には改行などの特殊文字を含めることができます。

  • \b for backspace
  • \f for form feed
  • \n for newline
  • \r for carriage return 
  • \t for tab
  • \” for double quote
  • \\ for backslash

For example:

例えば、

{
  "@@locale": "en",

  "helloWorld": "Hello\nWorld",
  "@helloWorld": {
    "description": "The conventional newborn programmer greeting"
  }
}

 




Messages With Parameters

It’s often useful to include application values in messages.

多くの場合、メッセージにアプリケーションの値を含めると便利です。

 

In the catalog, message parameters are defined with “placeholders”: parameter names bracketed with curly braces.

カタログでは、メッセージパラメータは「placeholders」で定義されています。パラメータ名は中括弧{}で囲まれています。

 

These placeholders become positional method parameters in the generated AppLocalizations class. Placeholder names must be valid Dart method parameter names.

これらのプレースホルダーは、生成されたAppLocalizationsクラスのメソッドのポジショナルパラメーターになります。 プレースホルダー名は、有効なDartメソッドのパラメーター名である必要があります。

 

Each placeholder must be defined in the “placeholders” object. For example, to define a hello message with a userName parameter:

各プレースホルダーは、「プレースホルダー」オブジェクトで定義する必要があります。 たとえば、userNameパラメーターを使用してhelloメッセージを定義するには、次のようにします。

"hello": "Hello {userName}",
"@hello": {
  "description": "A message with a single parameter",
  "placeholders": {
    "userName": {
      "type": “String”,
      “example”: “Bob”
    }
  }
}

The userName parameter has type String. The generated hello() method returns a String:

userNameパラメーターのタイプはStringです。 生成されたhello()メソッドは文字列を返します。

// Returns “Hello John”.
AppLocalizations.of(context).hello(“John”)

If a placeholder’s type is not defined, then the corresponding parameter has type Object, and its String value is computed with toString().

プレースホルダーの型が定義されていない場合、対応するパラメーターの型はObjectであり、その文字列値はtoString()で計算されます。

 

The placeholder’s example value is intended to help translators. In the future it might be used in generated tests.

プレースホルダーの値の例は、翻訳者を支援することを目的としています。 将来的には、生成されたテストで使用される可能性があります。工事中🏗

 

Messages can have as many parameters as you like, although too many parameters can make creating good translations difficult.

メッセージには必要な数のパラメーターを含めることができますが、パラメーターが多すぎると、適切な翻訳を作成するのが難しくなる可能性があります。

 

Below is an example with two parameters. The types of the parameters aren’t given, so they’ll be Object in the generated greeting() method. The greeting() method will convert the parameters to String with toString().

以下は、2つのパラメーターを使用した例です。 パラメータのタイプは指定されていないため、生成されたgreeting()メソッドではObject型になります。 Greeting()メソッドは、toString()を使用してパラメーターをStringに変換します。

"greeting": "{hello} {world}",
"@greeting": {
  "description": "A message with a two parameters",
  "placeholders": {
    "hello": {},
    "world": {}
  }
},

 

Just passing String valued arguments to the greeting() method is fine, because String.toString() is essentially a no-op.

String.toString()は本質的に何もしないので、文字列値の引数をgreeting()メソッドに渡すだけで問題ありません。

AppLocalizations.of(context).greeting(‘Hello’, ‘World’)

 


Messages With Numbers and Currencies

Numbers and numbers that represent currency values are displayed very differently in different locales.

「数値」と「通貨値を表す数値」は、ロケールによって表示が大きく異なります。

 

The Dart intl package provides support for formatting the strings they’re converted to.

Dartのintlパッケージは、変換される文字列のフォーマットをサポートします。

 

工事中🏗

 

 

 

参考

https://docs.google.com/document/d/10e0saTfAv32OZLRmONy866vnaw0I2jwL8zukykpgWBc/edit

Flutterの新しくて簡単な国際化

New and easy internationalization of Flutter apps

described in the official guide,

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です