繁体   English   中英

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

[英]String Constant as a Localized String

我想本地化我的常量。 常量被定义并以通常的方式声明:

extern NSString * const kStringName;

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

如何使其可本地化? 这只是无法工作......

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

谢谢!

const变量可能已在编译时进行了优化,因此您无法在运行时更改它。 你根本不能拥有const本地化字符串。

不完全恒定,但也很有用

//in the beginning of source file
static NSString*  CommentsTitleString;

@implementation ClassName

+(void)initialize
{
    CommentsTitleString =  NSLocalizedString(@"PLAYER_comments", nil);
}

@end

当你需要显示它时,你不能只定位你的常量吗?

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

我创建了一个PHP脚本,它将正确格式化的Localizable.strings文件作为输入,并生成一个Localizable.h文件作为输出,包含每个String-Key的相应#define-commands。 您可以根据需要进行修改。

该脚本希望所有字符串键都使用大写字母分割的子字格式化,因此在Localizable.strings文件中一行应如下所示:

"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."\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"; 
}

?>

这是你不能做的事情。

根据您尝试做的原因,可能一个好的解决方案是使用静态字符串变量。

通过在标头中使用extern声明并使用NSLocalizedString()在实现中定义来执行正常路由会导致此错误:

Initializer元素不是编译时常量

这是解决这个问题的一种方法。

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

@interface MyGlobals : NSObject

+ (NSString *)localizedStringWhatever;

@end

实施方法......

@implementation MyGlobals

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

@end

当你需要使用它时导入MyGlobals并询问它的字符串......

NSString *whatever = [MyGlobals localizedStringWhatever];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM