2011-04-07 80 views
10

我想本地化我的常量。常量被定义并声明为通常的方式:字符串常量作为本地化字符串

extern NSString * const kStringName; 

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

如何使其可本地化?这是行不通的...

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

谢谢!

回答

7

一个const变量可能已经在编译时优化了,所以你不能在运行时改变它。你根本不能拥有常量本地化的字符串。

1

这是你不能做的事情。

根据你为什么要尝试做,也许一个好的解决方案是使用静态字符串变量。

3

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

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

我已经创建了一个PHP脚本,需要正确格式化Localizable.strings文件作为输入,并产生一个Localizable.h文件作为包含每个字符串密钥适当#定义的命令输出。您可以根据需要修改它。

脚本要求所有的字符串键与大写字母拆分子言被格式化,所以行看起来应该像这样在你的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"; 
} 

?> 
3

不exactl Ÿ不变,也有用

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

@implementation ClassName 

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

@end 
0

通过在头部与extern声明和使用NSLocalizedString()导致这个错误的实现定义走向正常路线:

Initializer element is not a compile-time constant

下面就来解决这个问题的一种方式问题。

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

@interface MyGlobals : NSObject 

+ (NSString *)localizedStringWhatever; 

@end 

实现的方法...

@implementation MyGlobals 

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

@end 

当你需要使用它导入MyGlobals,并要求它为字符串...

NSString *whatever = [MyGlobals localizedStringWhatever];