tôi đã tạo ra một kịch bản PHP mà phải mất một tập tin Localizable.strings định dạng đúng như đầu vào và tạo ra một tập tin Localizable.h như đầu ra chứa thích hợp # define-lệnh cho mỗi String-Key. Bạn có thể sửa đổi nó khi bạn thấy phù hợp.
Kịch bản yêu cầu tất cả các phím chuỗi được định dạng với tiểu lời chia bằng chữ hoa, do đó, một dòng nên xem xét như thế này trong Localizable.strings tập tin của bạn:
"SectionSomeString" = "This is my string.";
mà sau đó sẽ được chuyển đổi sang
#define SECTION_SOME_STRING NSLocalizedString(@"SectionSomeString", nil)
Các kịch bản PHP trông như sau:
<?php
/**
Script for generating constants out of Localizable.strings files
Author: Gihad Chbib
*/
define("INPUT_FILE", "Localizable.strings");
define("OUTPUT_FILE", "Localizable.h");
define("HEADER_COMMENT", "// Auto-generated constants file - don't change manually!");
if (file_exists(INPUT_FILE)) {
$file = fopen(INPUT_FILE, "r");
$defineconstant = str_replace(".", "_", OUTPUT_FILE);
$output = HEADER_COMMENT."\n\n";
$output .= "#ifndef _".$defineconstant."\n";
$output .= "#define _".$defineconstant."\n";
while (!feof($file)) {
$lineOfText = fgets($file);
if ((strstr($lineOfText, "=") !== FALSE) && (substr($lineOfText, -2) === ";\n")) {
$arr = explode("=", $lineOfText);
$defineKey = str_replace("\"", "", $arr[0]);
$constructedKey = "";
for ($i=0; $i<strlen($defineKey); $i++) {
$letter = $defineKey[$i];
if (preg_match('/[a-z|A-Z]$/',$letter)==true) {
$ucletter = strtoupper($letter);
if (($ucletter === $letter) && ($i !== 0)) {
$constructedKey .= "_".$ucletter;
} else {
$constructedKey .= $ucletter;
}
} else {
$constructedKey .= $letter;
}
}
$defineKey = trim($defineKey);
$constructedKey = trim($constructedKey);
$output .= "#define $constructedKey NSLocalizedString(@\"$defineKey\", nil);\n";
} else if (substr($lineOfText, 0, 2) == "//") {
$output .= "\n$lineOfText\n";
}
}
$output .= "\n#endif\n";
echo nl2br($output);
fclose($file);
// Save file
file_put_contents(OUTPUT_FILE, $output, LOCK_EX);
} else {
echo "Input file ".INPUT_FILE." not found";
}
?>