作为本地化字符串的字符串常量



我想本地化我的常量。常量按常规方式定义和声明:

extern NSString * const kStringName;
NSString * const kStringName = @"Whatever...";

如何使其本地化?这就是不能工作…

NString * const kStringName = NSLocalizedString(@"Whatever...", @"Whatever...");

谢谢!

const变量可能已经在编译时进行了优化,因此您无法在运行时更改它。你不能有const localizedstring

当你需要显示常量时,你就不能本地化它吗?

[[NSBundle mainBundle] localizedStringForKey:kStringName 
                                       value:kStringName 
                                       table:nil]

不完全是常量,但也很有用

//in the beginning of source file
static NSString*  CommentsTitleString;
@implementation ClassName
+(void)initialize
{
    CommentsTitleString =  NSLocalizedString(@"PLAYER_comments", nil);
}
@end

我创建了一个PHP脚本,它接受正确格式的Localizable。字符串文件作为输入,并生成一个Localizable.h文件作为输出,其中为每个String-Key包含适当的#define-commands。你可以随意修改

该脚本期望所有字符串键都用大写字母分割的子字格式化,因此在Localizable中一行应该是这样的。字符串文件:

"SectionSomeString" = "This is my string.";

,然后转换为

#define SECTION_SOME_STRING NSLocalizedString(@"SectionSomeString", nil)
PHP脚本如下所示:
<?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."nn";
    $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$lineOfTextn";
        }
    }
    $output .= "n#endifn";
    echo nl2br($output);
    fclose($file);
    // Save file
    file_put_contents(OUTPUT_FILE, $output, LOCK_EX);
} else {
    echo "Input file ".INPUT_FILE." not found"; 
}
?>

这是你不能做的。

根据你想要做的具体原因,也许一个好的解决方案是使用一个静态字符串变量。

在头文件中使用extern声明,在实现中使用NSLocalizedString()定义,这会导致这个错误:

初始化器元素不是编译时常量

有一种方法可以解决这个问题。

在头文件中声明一个返回字符串的类方法…

@interface MyGlobals : NSObject
+ (NSString *)localizedStringWhatever;
@end

实现方法…

@implementation MyGlobals
+ (NSString *)localizedStringWhatever {
    return NSLocalizedString(@"Whatever", @"blah blah blah.");
}
@end

当您需要使用它时,导入MyGlobals并向它询问字符串…

NSString *whatever = [MyGlobals localizedStringWhatever];

最新更新