⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usavetranslation.pas

📁 DelphiDoc is a program for automatic generation of documentation on a Delphi-Project. At the momen
💻 PAS
📖 第 1 页 / 共 2 页
字号:
     begin
      Code[3] := '_';                            //use underscore (instead '-')
      Code[4] := UpCase(Code[4]);                //region should be upper case
      Code[5] := UpCase(Code[5]);
     end;
   end;
  FLanguageCode := Code;                     //assign the new language code
 end;

var       PreOptionCount   :Cardinal;         //number of inherited options
begin
 PreOptionCount := inherited GetOptionCount;  //get number of inherited ones
 if Index < PreOptionCount then               //asked for inherited option?
  inherited SetOption(Index, Value)             //forward to parent class
 else
  case Index - PreOptionCount of                //depending on index of option
    0: FLanguageNameEnglish := Value.StrData;     //set the option to the value
    1: FLanguageNameItself := Value.StrData;
    2: SetLanguageCode(Value.StrData);
    3: fTranslatorName := Value.StrData;
    4: FTranslationBaseLanguage := Value.StrData;
  else
   assert(Index >= GetOptionCount);
   raise EInvalidOption.Create('Invalid index for option supplied!');
  end;
end;













{Process parsed data; i.e. in this case save the parsed data to a file.
~result if the file has been successfully saved and it hasn't been aborted }
function TSaveTranslation.DoGenerateDocumentation: Boolean;
var      Translations    :TAllDocumentationTexts; //current translated texts

 {Reads the currently set translated texts. }
 procedure ReadCurrentTexts;
 var       dt         :TDocumentationTexts;    //counter through all texts
 begin
  for dt := Low(dt) to High(dt) do             //read all the texts
   Translations[dt] := DocumentationTexts[dt].T;
 end;

 {Compares the locally saved texts with the currently set translated texts
  (after setting them to the language the translation was based on). }
 procedure CompareTranslations;
 var       ID                 :TMessageID;          //ID of the messages
           dt                 :TDocumentationTexts; //counter through all texts
 begin
  ID := FSaveTranslationMessagesID;                 //cache the ID
  for dt := Low(dt) to High(dt) do                  //compare all the texts
   if Translations[dt] = DocumentationTexts[dt].T then //still the same?
    AddSimpleMessage(ID, Ord(stmkMaybeNotTranslated),    //log warning message
                     Format('The text "%s" (%s) might not have been translated, value: "%s"',
                            [DocumentationTexts[dt].N, DocumentationTexts[dt].C, DocumentationTexts[dt].T]));
 end;

 {(Re-)Sets the locally saved texts as the current translated texts. }
 procedure ResetTranslations;
 var       dt         :TDocumentationTexts;    //counter through all texts
 begin
  for dt := Low(dt) to High(dt) do             //assign all the texts
   DocumentationTexts[dt].T := Translations[dt];
 end;

 {Returns the current year. }
 function CurrentYear: Word;
 var      Month, Day :Word;                //ignored parts of the current date
 begin
  DecodeDate(Date, Result, Month, Day);    //get parts of the current date
 end;

var      f               :TextFile;      //to write the new unit file
         LanguageIdent   :String;        //name of language for identifiers
         i               :Integer;       //general counter
         dt              :TDocumentationTexts;  //counter through all texts
begin
 CreateDocumentationDirectory;           //create path of the main directory

 Progress.Reset;
 Progress.SetThrowExceptionOnStepIfAbort(True);

 Progress.SetWorkText('Checking Options...');
 Progress.SetProgressText('');
 Progress.SetProcessText('');

 Progress.SetMaximum(3);


 //all options set?
 if (FLanguageNameEnglish = '') or (FLanguageNameItself = '')  or
    (FLanguageCode = '') or (FTranslatorName = '') or
    (FTranslationBaseLanguage = '') then
  raise EInvalidOption.Create('At least one of the options has not been initialized!');

 //extract valid characters from name of language
 LanguageIdent := '';
 for i := 1 to Length(FLanguageNameEnglish) do
  if FLanguageNameEnglish[i] in IdentifierChars then
   LanguageIdent := LanguageIdent + FLanguageNameEnglish[i];

 //enough valid characters for an (unique) identifier
 if Length(LanguageIdent) <= 2 then
  raise EInvalidOption.Create('The English name of the language is invalid, it has to contain at least three English characters!');




 Progress.StepProgress;
 Progress.SetWorkText('Saving new unit file...');



 //read currently set translated texts
 ReadCurrentTexts;

 try
   //set language to the language the translation was based on
   with TDocumentationTextOptionWrapper.Create do
    try
      SetOptionByName('Language', FTranslationBaseLanguage);
    finally
     Free;
    end;

   //compare translated texts to those of the base language
   CompareTranslations;
 finally
  //and set the translated texts again
  ResetTranslations;
 end;



 Progress.StepProgress;
 Progress.SetWorkText('Saving new unit file...');


 //open the file of the unit
 AssignFile(f, FDestPath + 'U' + LanguageIdent + 'Texts.pas');
 Rewrite(f);
 try
   //write general header

   WriteLn(f, '{  JADD - Just Another DelphiDoc: Documentation from Delphi Source Code');
   WriteLn(f);
   WriteLn(f, 'Copyright (C) 2003-2007   Gerold Veith');
   WriteLn(f, 'Translation   ', CurrentYear, ' by     ', FTranslatorName);
   WriteLn(f);
   WriteLn(f, 'This file is part of JADD - Just Another DelphiDoc.');
   WriteLn(f);
   WriteLn(f, 'DelphiDoc is free software: you can redistribute it and/or modify');
   WriteLn(f, 'it under the terms of the GNU General Public License version 3 as');
   WriteLn(f, 'published by the Free Software Foundation.');
   WriteLn(f);
   WriteLn(f, 'DelphiDoc is distributed in the hope that it will be useful,');
   WriteLn(f, 'but WITHOUT ANY WARRANTY; without even the implied warranty of');
   WriteLn(f, 'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the');
   WriteLn(f, 'GNU General Public License for more details.');
   WriteLn(f);
   WriteLn(f, 'You should have received a copy of the GNU General Public License');
   WriteLn(f, 'along with this program.  If not, see <http://www.gnu.org/licenses/>.');
   WriteLn(f, '}');
   WriteLn(f);
   WriteLn(f, 'unit U', LanguageIdent, 'Texts;');
   WriteLn(f);
   WriteLn(f, '{Contains the texts used in the documentation localized to ',
              FLanguageNameEnglish, '. }');
   WriteLn(f);
   WriteLn(f, 'interface');
   WriteLn(f);
   WriteLn(f, 'implementation');
   WriteLn(f);
   WriteLn(f, 'uses UDocumentationTexts;');
   WriteLn(f);
   WriteLn(f);
   WriteLn(f, '      //constant containing values for each text of the generators that can be');
   WriteLn(f, '      //changed/localized in ', FLanguageNameEnglish);
   WriteLn(f, '      //~see DocumentationTexts may be assigned to that structure');
   WriteLn(f, 'const DocumentationTexts', LanguageIdent,
              ': TAllDocumentationTexts =');
   WriteLn(f, '           (');

   //write all translated texts
   for dt := Low(dt) to Pred(High(dt)) do      //read all the texts
    WriteLn(f, '            ', QuotedStr(Translations[dt]) , ',');
   WriteLn(f, '            ', QuotedStr(Translations[High(dt)]));

   //write the footer
   WriteLn(f, '           );');
   WriteLn(f);
   WriteLn(f);
   WriteLn(f, 'initialization');
   WriteLn(f, ' //register the ', FLanguageNameEnglish,
              ' translation for the documentation');
   WriteLn(f, ' RegisterLocalizedTexts(@DocumentationTexts', LanguageIdent,
              ',');
   WriteLn(f, '                        [', QuotedStr(FLanguageCode), ', ',
              QuotedStr(FLanguageNameEnglish), ', ',
              QuotedStr(FLanguageNameItself), ']);');
   WriteLn(f);
   WriteLn(f, 'end.');

 finally
  CloseFile(f);                 //close the file
 end;



 Progress.StepProgress;

 Progress.Reset;
 Progress.SetWorkText('Finished writing file!');
 Progress.SetProgressText('Finished!');
 Progress.SetProcessText('');
 Progress.SetMaximum(1);
 Progress.StepProgress;

 Result := True;
end;






      //descriptions of the messages of the class ~[link TSaveTranslation]
const SaveTranslationMessages: array[TSaveTranslationMessageKind] of
                                                          TMessageDescription =
      (
       (Text:
'A text might not have been translated.';
        HelpText:
'This simple warning message is generated for each translated text that does not differ from the original text in the selected base language.' + LineDelimiter +
'It may very well be, that it has the same translation (after all one would choose the language most alike as the base language) or it is the name of an untranslated keyword of Pascal.' + LineDelimiter + 
'So check it, if this is the case, the message can be ignored.';
        Seriousness: msWarning;
       )
      );


initialization
 //create object to manage the messages that can be generated by
 //~[link TSaveTranslation]
 SaveTranslationMessageDescriptions :=
                           TMessageDescriptions.Create(SaveTranslationMessages,
                                                       'TSaveTranslation');

{$IFOPT C+}
 TSaveTranslation.Create.Destroy;         //generate warning, if class abstract
{$ENDIF}
 AddGeneratorClass(TSaveTranslation);     //register "generator" class


finalization
 SaveTranslationMessageDescriptions.Free; //free list of messages

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -