Utworzyłem skrypt PHP, który pobiera prawidłowo sformatowany plik Localizable.strings jako wejścia i wyjął plik Localizable.h jako wyjście zawierające odpowiednie #define komend dla każdego String-Key. Możesz go zmodyfikować według własnego uznania.
Skrypt oczekuje, że wszystkie klucze łańcuch zostanie sformatowana z podrzędnych słów rozbiciu wielkich liter, a więc linia powinna wyglądać w pliku Localizable.strings:
"SectionSomeString" = "This is my string.";
które następnie przekształcić
#define SECTION_SOME_STRING NSLocalizedString(@"SectionSomeString", nil)
skrypt PHP wygląda następująco:
<?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";
}
?>