2011-04-07 25 views
10

私の定数をローカライズしたいと思います。定数は定義され、通常の方法で宣言されます。ローカライズされた文字列としての文字定数

extern NSString * const kStringName; 

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

ローカライズする方法を教えてください。これはちょうどうまくいかない...

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

ありがとう!

答えて

7

const変数はコンパイル時に既に最適化されているため、実行時に変更することはできません。ローカライズされた文字列をconstにすることはできません。

1

これはあなたができないものです。

正確に何をしようとしているかによって、静的な文字列変数を使用することをお勧めします。

3

表示する必要があるときに定数をローカライズすることはできませんか?

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

私は、入力として正しくフォーマットのLocalizable.stringsファイルを取り、PHPスクリプトを作成し、各文字列のキーのための適切な#定義-コマンドを含む出力としてLocalizable.hファイルを作成しました。あなたはそれが適切と思われるようにそれを変更することができます。

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

その後、

に変換されます:

スクリプトは、すべての文字列キーがとてもラインがあなたのLocalizable.stringsファイルに次のようになり、大文字によって分割サブ言葉でフォーマットされることを想定してい

#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ありませんY定数だけでなく、

//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 

メソッドを実装します...あなたはそれがMyGlobalsをインポートし、それを聞いて使用する必要が

@implementation MyGlobals 

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

@end 

文字列の場合...

NSString *whatever = [MyGlobals localizedStringWhatever]; 
関連する問題