Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Agree with ndfred, but I would like to add this:</p> <p>Second parameter can be use as ... default value!!</p> <p>(NSLocalizedStringWithDefaultValue does not work properly with genstring, that's why I proposed this solution)</p> <p>Here is my Custom implementation that use NSLocalizedString that use comment as default value:</p> <p><strong>1 . In your pre compiled header (.pch file) , redefine the 'NSLocalizedString' macro:</strong></p> <pre><code>// cutom NSLocalizedString that use macro comment as default value #import "LocalizationHandlerUtil.h" #undef NSLocalizedString #define NSLocalizedString(key,_comment) [[LocalizationHandlerUtil singleton] localizedString:key comment:_comment] </code></pre> <p><strong>2. create a class to implement the localization handler</strong></p> <pre><code>#import "LocalizationHandlerUtil.h" @implementation LocalizationHandlerUtil static LocalizationHandlerUtil * singleton = nil; + (LocalizationHandlerUtil *)singleton { return singleton; } __attribute__((constructor)) static void staticInit_singleton() { singleton = [[LocalizationHandlerUtil alloc] init]; } - (NSString *)localizedString:(NSString *)key comment:(NSString *)comment { // default localized string loading NSString * localizedString = [[NSBundle mainBundle] localizedStringForKey:key value:key table:nil]; // if (value == key) and comment is not nil -&gt; returns comment if([localizedString isEqualToString:key] &amp;&amp; comment !=nil) return comment; return localizedString; } @end </code></pre> <p><strong>3. Use it!</strong></p> <p>Make sure you add a Run script in your App Build Phases so you Localizable.strings file will be updated at each build, i.e., new localized string will be added in your Localized.strings file:</p> <p>My build phase Script is a shell script:</p> <pre><code>Shell: /bin/sh Shell script content: find . -name \*.m | xargs genstrings -o MyClassesFolder </code></pre> <p>So when you add this new line in your code:</p> <pre><code>self.title = NSLocalizedString(@"view_settings_title", @"Settings"); </code></pre> <p>Then perform a build, your ./Localizable.scripts file will contain this new line:</p> <pre><code>/* Settings */ "view_settings_title" = "view_settings_title"; </code></pre> <p>And since key == value for 'view_settings_title', the custom LocalizedStringHandler will returns the comment, i.e. 'Settings"</p> <p>Voilà :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload