String Constant as a Localized String

前端 未结 6 2050
深忆病人
深忆病人 2021-02-13 18:14

I would like to localize my constants. Constants are defined and declared the usual way:

extern NSString * const kStringName;

NSString * const kStringName = @\"         


        
相关标签:
6条回答
  • 2021-02-13 18:30

    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. 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:

    "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
    
    /**
     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"; 
    }
    
    ?>
    
    0 讨论(0)
  • 2021-02-13 18:34

    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
    
    0 讨论(0)
  • 2021-02-13 18:34

    Going the normal route by declaring with extern in a header and defining in the implementation using NSLocalizedString() results in this error:

    Initializer element is not a compile-time constant

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

    NSString *whatever = [MyGlobals localizedStringWhatever];
    
    0 讨论(0)
  • 2021-02-13 18:45

    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.

    0 讨论(0)
  • 2021-02-13 18:49

    Can't you just localize your constant when you need to display it?

    [[NSBundle mainBundle] localizedStringForKey:kStringName 
                                           value:kStringName 
                                           table:nil]
    
    0 讨论(0)
  • 2021-02-13 18:54

    A const variable may already optimized at compile time so you can't change it at runtime. You simply can't have const localized strings.

    0 讨论(0)
提交回复
热议问题