2011-04-07 121 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];