简体   繁体   English

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

[英]String Constant as a Localized String

I would like to localize my constants. 我想本地化我的常量。 Constants are defined and declared the usual way: 常量被定义并以通常的方式声明:

extern NSString * const kStringName;

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

How to make it localizable? 如何使其可本地化? This just can not work... 这只是无法工作......

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

Thank you! 谢谢!

A const variable may already optimized at compile time so you can't change it at runtime. const变量可能已在编译时进行了优化,因此您无法在运行时更改它。 You simply can't have const localized strings. 你根本不能拥有const本地化字符串。

Not exactly constant, but also useful 不完全恒定,但也很有用

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

@implementation ClassName

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

@end

Can't you just localize your constant when you need to display it? 当你需要显示它时,你不能只定位你的常量吗?

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

I've created a PHP script that takes a correctly formatted Localizable.strings file as input and produced a Localizable.h file as output containing appropriate #define-commands for each String-Key. 我创建了一个PHP脚本,它将正确格式化的Localizable.strings文件作为输入,并生成一个Localizable.h文件作为输出,包含每个String-Key的相应#define-commands。 You can modify it as you see fit. 您可以根据需要进行修改。

The script expects all string keys to be formatted with sub-words split by uppercase letters, so a line should look like this in your Localizable.strings file: 该脚本希望所有字符串键都使用大写字母分割的子字格式化,因此在Localizable.strings文件中一行应如下所示:

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

which would then be converted to 然后将转换为

#define SECTION_SOME_STRING NSLocalizedString(@"SectionSomeString", nil)

The PHP script looks as follows: 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"; 
}

?>

This is something you can't do. 这是你不能做的事情。

Depending on why exactly you are trying to do, maybe a good solution would be to use a static string variable. 根据您尝试做的原因,可能一个好的解决方案是使用静态字符串变量。

Going the normal route by declaring with extern in a header and defining in the implementation using NSLocalizedString() results in this error: 通过在标头中使用extern声明并使用NSLocalizedString()在实现中定义来执行正常路由会导致此错误:

Initializer element is not a compile-time constant Initializer元素不是编译时常量

Here's one way to get around this problem. 这是解决这个问题的一种方法。

In the header file declare a class method that returns a string... 在头文件中声明一个返回字符串的类方法...

@interface MyGlobals : NSObject

+ (NSString *)localizedStringWhatever;

@end

Implement the method... 实施方法......

@implementation MyGlobals

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

@end

When you need to use it import MyGlobals and ask it for the string... 当你需要使用它时导入MyGlobals并询问它的字符串......

NSString *whatever = [MyGlobals localizedStringWhatever];

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

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