2011-02-13 72 views
3

我正在写一个AppleScript的来解析的iOS本地化文件(/en.lproj/Localizable.strings),翻译值和输出的翻译(/fr.lproj/Localizable.strings)到磁盘UTF-16(Unicode)编码。如何使用AppleScript创建和编写UTF-16文本文件?

出于某种原因,生成的文件在每个字母之间都有一个额外的空格。在进行了一些挖掘之后,我在“了解AppleScript:脚本综合指南”中找到了问题的原因。

“如果你不小心读了UTF-16文件 为的MacRoman,结果值可以 外观乍一看像一个普通的 字符串,尤其是如果它包含 英文文本,你很快就会发现 那但当您尝试使用 时,出现以下错误: 症状是您的“字符串”中的每个可见字符 在它前面似乎都有一个不可见字符 例如,读取UTF-16编码 包含短语“你好”的文本文件3210 World!“作为一个字符串产生一个字符串 ,比如”H l l l o W o r l d! “,其中每个” “实际上是一个不可见的ASCII 0字符。”

因此,例如,我的英语本地化字符串文件有:

"Yes" = "Yes"; 

而产生的法语本地化字符串文件:

" Y e s " = " O u i " ; 

这是我CREATEFILE方法:

on createFile(fileFolder, fileName) 
    tell application "Finder" 
     if (exists file fileName of folder fileFolder) then 
      set the fileAccess to open for access file fileName of folder fileFolder with write permission 
      set eof of fileAccess to 0 
      write ((ASCII character 254) & (ASCII character 255)) to fileAccess starting at 0 
      --write «data rdatFEFF» to fileAccess starting at 0 
      close access the fileAccess 
     else 
      set the filePath to make new file at fileFolder with properties {name:fileName} 
      set the fileAccess to open for access file fileName of folder fileFolder with write permission 
      write ((ASCII character 254) & (ASCII character 255)) to fileAccess starting at 0 
      --write «data rdatFEFF» to fileAccess starting at 0 
      close access the fileAccess 
     end if 
     return file fileName of folder fileFolder as text 
    end tell 
end createFile 

这里是我的WriteFile的方法:

on writeFile(filePath, newLine) 
    tell application "Finder" 
     try 
      set targetFileAccess to open for access file filePath with write permission 
      write newLine to targetFileAccess as Unicode text starting at eof 
      close access the targetFileAccess 
      return true 
     on error 
      try 
       close access file filePath 
      end try 
      return false 
     end try 
    end tell 
end writeFile 

任何想法,我做错了吗?

+0

我认为手动翻译这将花费更少的时间比写这个AppleScript甚至不工作。 :') – 2011-02-13 00:49:09

+0

Radek,超过150多个短语需要翻译成10多种语言...... – 2011-02-13 01:02:18

回答

0

下面是我用来读写UTF16的处理程序。您不需要单独的“创建文件”处理程序。如果文件不存在,写入处理程序将创建该文件。将“appendText”变量设置为true或false。 False表示覆盖文件,true表示将新文本添加到文件中当前文本的末尾。我希望这有帮助。

on writeTo_UTF16(targetFile, theText, appendText) 
    try 
     set targetFile to targetFile as text 
     set openFile to open for access file targetFile with write permission 
     if appendText is false then 
      set eof of openFile to 0 
      write (ASCII character 254) & (ASCII character 255) to openFile starting at eof -- UTF-16 BOM 
     else 
      tell application "Finder" to set fileExists to exists file targetFile 
      if fileExists is false then 
       set eof of openFile to 0 
       write (ASCII character 254) & (ASCII character 255) to openFile starting at eof -- UTF-16 BOM 
      end if 
     end if 
     write theText to openFile starting at eof as Unicode text 
     close access openFile 
     return true 
    on error theError 
     try 
      close access file targetFile 
     end try 
     return theError 
    end try 
end writeTo_UTF16 

on readFrom_UTF16(targetFile) 
    try 
     set targetFile to targetFile as text 
     targetFile as alias -- if file doesn't exist then you get an error 
     set openFile to open for access file targetFile 
     set theText to read openFile as Unicode text 
     close access openFile 
     return theText 
    on error 
     try 
      close access file targetFile 
     end try 
     return false 
    end try 
end readFrom_UTF16 
0

如果您收到的每一个字符之间的实际空间,你可能已经拿到了“(字符我通someText的j)的为字符串”在你的代码的反模式[1]。这将把一个字符串分割成一个字符列表,然后将它强制回到一个字符串中,并在每个字符之间插入当前的文本项分隔符。正确的(即快速和安全的)获取子字符串的方法是:'通过某些文本的文本'(p179-181)。

OTOH,如果你得到隐形字符之间的每个字符[2],那么是的,这将是一个编码问题,通常使用MacRoman或其他单字节编码读取UTF16编码文件。如果你的文件有一个有效的Byte Order Mark,那么任何Unicode精通的文本编辑器都应该使用正确的编码来读取它。


[1] p179指出,这个习语是不安全的,但忘了提供它引起的问题的实际演示。 [3]

[2] IIRC在p501上的例子是为了使用矩形符号来表示不可见的字符,即“⃞H⃞e⃞l⃞l⃞o”而不是“H ello”,但没有完全出现,所以可能被误读为意味着可见空间。 [3]

[3]随意向Apress提交勘误表。