简体   繁体   中英

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. You simply can't have const localized strings.

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. 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"; 
}

?>

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:

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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