【问题标题】:How to transform a string from a delphi code source into a standard string to be saved in a database?如何将字符串从 delphi 代码源转换为标准字符串以保存在数据库中?
【发布时间】:2021-01-25 12:05:13
【问题描述】:

我编写了一个解析器来从我的 Delphi 代码中提取资源字符串,以将它们保存到我应该发送给翻译器的数据库中。

我的要求是:有没有一种简单的方法可以将 Delphi 字符串从源代码转换为准备保存到数据库中的“标准”字符串?

这是一个例子:从我的源代码中我有这个:

MyResourceString1 = 'Error : I can''t load the file from disk !';

或

MyResourceString2 = 'Error : I can'#39't load the file from disk !';

然后在我的解析器处理之后,我提取了这些字符串值。但在将它们保存到数据库之前,我想将所有字符串转换为“标准”句子,例如:

"Error : I can't load the file from disk !"

或

"Error : I can't load the file from disk !"

我想使用与解析 Delphi 字符串不同的解释器函数将所有#code char 更改为正确的字符。

感谢您的帮助。

【问题讨论】:

  • 如果字符串来自Delphi源代码,那么有很多可能性,你需要一个编译器来确保抓住一切。除非您提供的示例是全部(字符串和字符常量的简单连接)。您可以从可执行文件中提取资源字符串,而不是从源代码中获取最终编译的形式。
  • @AmigoJack :这正是我需要将我的法语字符串发送给翻译的原因......感谢链接
  • @fpiette :我需要从源代码中获取,因为从可执行文件中,我的资源字符串标识符变成了一个整数。我的 Delphi 解析器提取资源字符串以由翻译器将它们转换为可编辑的描述,然后通过返回,我生成另一个包含翻译的 Delphi 单元(顺便说一下,我需要标识符名称)。所以我的理解是,我需要用正确的字符替换所有 unicode 整数代码(如'#39#')......
  • 这不会回答您的问题,但我使用 dxGetText 来翻译我的应用程序,您遇到的所有问题以及更多问题都已通过该工具解决。有一个工具可以扫描所有源文件和dfm文件以提取字符串、资源字符串和组件属性中的字符串。你会得到一个特定格式的文本文件。然后是用于翻译的 poEdit 应用程序。见dxgettext.po.dk/Home

标签: delphi


【解决方案1】:

这很容易使用简单的状态机来实现:

function TryParseStringLiteral(const ALiteral: string; out AString: string): Boolean;
var
  LLiteral: string;
  i, c: Integer;
  InQuotes: Boolean;
  InCharCode: Boolean;
  NumCodeStr: string;
  NumVal: Integer;
begin

  LLiteral := ALiteral.Trim;

  SetLength(AString, LLiteral.Length);

  InQuotes := False;
  InCharCode := False;
  NumCodeStr := '';
  NumVal := 0;
  c := 0;
  for i := 1 to LLiteral.Length do
  begin

    if InCharCode then
    begin
      if (LLiteral[i] = '#') or (LLiteral[i] = '''') then
      begin
        if TryStrToInt(NumCodeStr, NumVal) and InRange(NumVal, Word.MinValue, Word.MaxValue) then
        begin
          Inc(c);
          AString[c] := Chr(NumVal);
        end
        else
          Exit(False);
        InCharCode := LLiteral[i] = '#';
        InQuotes := LLiteral[i] = '''';
        NumCodeStr := '';
        NumVal := 0;
      end
      else
        NumCodeStr := NumCodeStr + LLiteral[i];
    end
    else if InQuotes then
    begin
      if (LLiteral[i] = '''') and (i < LLiteral.Length) and (LLiteral[Succ(i)] = '''') then
      begin
        InQuotes := False; // a bit of a hack
        Inc(c);
        AString[c] := '''';
      end
      else if LLiteral[i] = '''' then
        InQuotes := False
      else
      begin
        Inc(c);
        AString[c] := LLiteral[i];
      end;
    end
    else if LLiteral[i] = '''' then
      InQuotes := True
    else if LLiteral[i] = '#' then
      InCharCode := True
    else
      Exit(False);

  end;

  if InCharCode then
    if TryStrToInt(NumCodeStr, NumVal) and InRange(NumVal, Word.MinValue, Word.MaxValue) then
    begin
      Inc(c);
      AString[c] := Chr(NumVal);
    end
    else
      Exit(False);

  if InQuotes then
    Exit(False);

  SetLength(AString, c);
  Result := True;

end;

尝试一下:

procedure TForm1.Edit1Change(Sender: TObject);
var
  S: string;
begin
  if TryParseStringLiteral(Edit1.Text, S) then
    Edit2.Text := S
  else
    Edit2.Text := ' -- Invalid string literal -- ';
end;

一些例子:

'This is a test!'                    This is a test!
'What''s up?'                        What's up?
'alpha'#64'beta'#63                  alpha@beta?
'alpha'#$40'beta'#$3F                alpha@beta?
#94#94#94                            ^^^
#94#94#94'ABC'                       ^^^ABC
'ABC'#94#94#94                       ABC^^^
'ABC'#94#94#94'abc'                  ABC^^^abc
'ABC'#94#94#94'abc'#63'!'            ABC^^^abc?!
'test'#32'again'                     test again
'test'#32#32#$20'again'              test   again
''                                   
''''                                 '
''''''''                             '''
'a'                                  a
#65                                  A
#65#$21                              A!
'''test'''#65                        'test'A

【讨论】:

  • 万分感谢。这正是我正在寻找的。我会在几个小时内对其进行测试,然后我会回复你,以确认它就像一个魅力。;-)
  • 我可以想象,是否有一个等效的反向函数可以将字符串转换为 unicode ?
猜你喜欢
  • 1970-01-01
  • 2012-05-19
  • 2012-01-26
  • 1970-01-01
  • 2011-12-06
  • 2010-09-17
  • 2017-10-08
  • 1970-01-01
相关资源
最近更新 更多