Example - Translating messages

Top  Previous  Next

ユーザーがempdept テーブルを更新できるようなフォームでは、サーバー上の幾つかの制約に違反するかもしれません。もしOnTranslateMessageイベントハンドラーがなければ、複製のemployee numberを挿入するためのメッセージは:

"ORA-00001: unique constraint (SCOTT.EMP_PRIMARY_KEY) violated"

 

この種の技術的なメッセージは、このようなOnTranslateMessageを作成することによってユーザーフレンドリーなメッセージに翻訳されます:

procedure TMainForm.TranslateMessage(Sender: TOracleDataSet; ErrorCode: Integer; const ConstraintName: string; Action: Char; var Msg: string);

begin

if (ConstraintName = 'EMP_PRIMARY_KEY') then

   Msg := 'An employee with this number already exists';

if (ConstraintName = 'DEPT_PRIMARY_KEY') then

   Msg := 'A department with this number already exists';

if (ConstraintName = 'EMP_FOREIGN_KEY') and (ErrorCode = 2291) then

   Msg := 'Department does not exist';

if (ConstraintName = 'EMP_FOREIGN_KEY') and (ErrorCode = 2292) then

   Msg := 'There are still employees in this department';

end;

 

ConstraintNameは制約に違反したことを示します。ErrorCodeはどのようにforeign key制約に違反したのかを特定するのを助けます。

実際の場面では、メッセージを翻訳するためにデータセット内にメッセージテーブルを作りたいでしょう。この方法で、制約をデータセットに、メッセージをこのテーブルに追加できます。そしてアプリケーションはそれを再コンパイルせずに拾い上げます。