SKLocalization.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // SKLocalization.m
  3. // Skim
  4. //
  5. // Created by Christiaan Hofman on 3/13/10.
  6. /*
  7. This software is Copyright (c) 2010-2018
  8. Christiaan Hofman. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in
  16. the documentation and/or other materials provided with the
  17. distribution.
  18. - Neither the name of Christiaan Hofman nor the names of any
  19. contributors may be used to endorse or promote products derived
  20. from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #import "SKLocalization.h"
  34. static NSString *localizedStringFromTable(NSString *string, NSString *table) {
  35. if ([string length] == 0)
  36. return nil;
  37. // we may want to check for missing localized strings when DEBUG
  38. return [[NSBundle mainBundle] localizedStringForKey:string value:@"" table:table];
  39. }
  40. #define LOCALIZE_PROPERTY_FROM_TABLE(property, table) \
  41. do { \
  42. NSString *value = localizedStringFromTable(property, table); \
  43. if (value) property = value; \
  44. } while (0)
  45. #define localizeTitleForObjectFromTable(object, table) LOCALIZE_PROPERTY_FROM_TABLE(object.title, table)
  46. #define localizeAlternateTitleForObjectFromTable(object, table) LOCALIZE_PROPERTY_FROM_TABLE(object.alternateTitle, table)
  47. #define localizeStringValueForObjectFromTable(object, table) LOCALIZE_PROPERTY_FROM_TABLE(object.stringValue, table)
  48. #define localizePlaceholderStringForObjectFromTable(object, table) LOCALIZE_PROPERTY_FROM_TABLE(object.placeholderString, table)
  49. #define localizeLabelForObjectFromTable(object, table) LOCALIZE_PROPERTY_FROM_TABLE(object.label, table)
  50. #define localizeToolTipForObjectFromTable(object, table) LOCALIZE_PROPERTY_FROM_TABLE(object.toolTip, table)
  51. @implementation NSObject (SKLocalization)
  52. - (void)localizeStringsFromTable:(NSString *)table {}
  53. @end
  54. @implementation NSArray (SKLocalization)
  55. - (void)localizeStringsFromTable:(NSString *)table {
  56. [self makeObjectsPerformSelector:_cmd withObject:table];
  57. }
  58. @end
  59. @implementation NSButtonCell (SKLocalization)
  60. - (void)localizeStringsFromTable:(NSString *)table {
  61. localizeTitleForObjectFromTable(self, table);
  62. localizeAlternateTitleForObjectFromTable(self, table);
  63. }
  64. @end
  65. @implementation NSPopUpButtonCell (SKLocalization)
  66. - (void)localizeStringsFromTable:(NSString *)table {
  67. // don't call super because the title is taken from the menu
  68. [[self menu] localizeStringsFromTable:table];
  69. }
  70. @end
  71. @implementation NSSegmentedCell (SKLocalization)
  72. - (void)localizeStringsFromTable:(NSString *)table {
  73. NSUInteger i, iMax = [self segmentCount];
  74. NSString *string;
  75. for (i = 0; i < iMax; i++) {
  76. if ((string = localizedStringFromTable([self labelForSegment:i], table)))
  77. [self setLabel:string forSegment:i];
  78. if ((string = localizedStringFromTable([self toolTipForSegment:i], table)))
  79. [self setToolTip:string forSegment:i];
  80. [[self menuForSegment:i] localizeStringsFromTable:table];
  81. }
  82. }
  83. @end
  84. @implementation NSTextFieldCell (SKLocalization)
  85. - (void)localizeStringsFromTable:(NSString *)table {
  86. localizeStringValueForObjectFromTable(self, table);
  87. localizePlaceholderStringForObjectFromTable(self, table);
  88. }
  89. @end
  90. @implementation NSView (SKLocalization)
  91. - (void)localizeStringsFromTable:(NSString *)table {
  92. localizeToolTipForObjectFromTable(self, table);
  93. [[self subviews] localizeStringsFromTable:table];
  94. }
  95. @end
  96. @implementation NSBox (SKLocalization)
  97. - (void)localizeStringsFromTable:(NSString *)table {
  98. [super localizeStringsFromTable:table];
  99. localizeTitleForObjectFromTable(self, table);
  100. }
  101. @end
  102. @implementation NSControl (SKLocalization)
  103. - (void)localizeStringsFromTable:(NSString *)table {
  104. [super localizeStringsFromTable:table];
  105. [[self cell] localizeStringsFromTable:table];
  106. }
  107. @end
  108. @implementation NSMatrix (SKLocalization)
  109. - (void)localizeStringsFromTable:(NSString *)table {
  110. [super localizeStringsFromTable:table];
  111. NSArray *cells = [self cells];
  112. NSString *toolTip;
  113. [cells localizeStringsFromTable:table];
  114. for (id cell in cells) {
  115. if ((toolTip = localizedStringFromTable([self toolTipForCell:cell], table)))
  116. [self setToolTip:toolTip forCell:cell];
  117. }
  118. }
  119. @end
  120. @implementation NSTabView (SKLocalization)
  121. - (void)localizeStringsFromTable:(NSString *)table {
  122. [super localizeStringsFromTable:table];
  123. [[self tabViewItems] localizeStringsFromTable:table];
  124. }
  125. @end
  126. @implementation NSTabViewItem (SKLocalization)
  127. - (void)localizeStringsFromTable:(NSString *)table {
  128. localizeLabelForObjectFromTable(self, table);
  129. [[self view] localizeStringsFromTable:table];
  130. }
  131. @end
  132. @implementation NSTableView (SKLocalization)
  133. - (void)localizeStringsFromTable:(NSString *)table {
  134. [super localizeStringsFromTable:table];
  135. [[self tableColumns] localizeStringsFromTable:table];
  136. [[self cornerView] localizeStringsFromTable:table];
  137. }
  138. @end
  139. @implementation NSTableColumn (SKLocalization)
  140. - (void)localizeStringsFromTable:(NSString *)table {
  141. [[self dataCell] localizeStringsFromTable:table];
  142. [[self headerCell] localizeStringsFromTable:table];
  143. }
  144. @end
  145. @implementation NSMenu (SKLocalization)
  146. - (void)localizeStringsFromTable:(NSString *)table {
  147. localizeTitleForObjectFromTable(self, table);
  148. [[self itemArray] localizeStringsFromTable:table];
  149. }
  150. @end
  151. @implementation NSMenuItem (SKLocalization)
  152. - (void)localizeStringsFromTable:(NSString *)table {
  153. localizeTitleForObjectFromTable(self, table);
  154. [[self submenu] localizeStringsFromTable:table];
  155. }
  156. @end
  157. @implementation NSWindow (SKLocalization)
  158. - (void)localizeStringsFromTable:(NSString *)table {
  159. localizeTitleForObjectFromTable(self, table);
  160. [[self contentView] localizeStringsFromTable:table];
  161. }
  162. @end