Skip to content

Commit

Permalink
支持本地化(国际化)
Browse files Browse the repository at this point in the history
支持本地化(国际化)
  • Loading branch information
ko1o committed Dec 26, 2016
1 parent 0767991 commit 9b6cf21
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 84 deletions.
15 changes: 15 additions & 0 deletions PYSearch/NSString+PYSearchExtension.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// 代码地址: https://github.com/iphone5solo/PYSearch
// 代码地址: http://www.code4app.com/thread-11175-1-1.html
// Created by CoderKo1o.
// Copyright © 2016年 iphone5solo. All rights reserved.
// NSString 分类

#import <Foundation/Foundation.h>

@interface NSString (PYSearchExtension)

/** 获取本地化字符串 */
- (NSString *)py_localizedString;

@end
42 changes: 42 additions & 0 deletions PYSearch/NSString+PYSearchExtension.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// 代码地址: https://github.com/iphone5solo/PYSearch
// 代码地址: http://www.code4app.com/thread-11175-1-1.html
// Created by CoderKo1o.
// Copyright © 2016年 iphone5solo. All rights reserved.
//

#import "NSString+PYSearchExtension.h"

@implementation NSString (PYSearchExtension)

- (NSString *)py_localizedString
{
return [self py_localizedStringForKey:self value:nil];
}

- (NSString *)py_localizedStringForKey:(NSString *)key value:(NSString *)value
{
static NSBundle *bundle = nil;
if (bundle == nil) {
// 只处理en、zh-Hans、zh-Hant三种情况,其他按照系统默认处理
NSString *language = [NSLocale preferredLanguages].firstObject;
if ([language hasPrefix:@"en"]) {
language = @"en";
} else if ([language hasPrefix:@"zh"]) {
if ([language rangeOfString:@"Hans"].location != NSNotFound) {
language = @"zh-Hans"; // 简体中文
} else { // zh-Hant\zh-HK\zh-TW
language = @"zh-Hant"; // 繁體中文
}
} else {
language = @"en";
}

// 从PYSearch.bundle中查找资源
bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"PYSearch" ofType:@"bundle"]];
}
value = [bundle localizedStringForKey:key value:value table:nil];
return [[NSBundle mainBundle] localizedStringForKey:key value:value table:nil];
}

@end
6 changes: 0 additions & 6 deletions PYSearch/PYSearch.bundle/PYSearch.h

This file was deleted.

61 changes: 0 additions & 61 deletions PYSearch/PYSearch.bundle/Root.plist

This file was deleted.

8 changes: 8 additions & 0 deletions PYSearch/PYSearch.bundle/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"PYSearchSearchPlaceholderText" = "Search content";
"PYSearchHotSearchText" = "Popular searches";
"PYSearchSearchHistoryText" = "Search history";
"PYSearchEmptySearchHistoryText" = "Clear the search history";

"PYSearchEmptyButtonText" = "Clear";
"PYSearchEmptySearchHistoryLogText" = "Clear the search history";
"PYSearchCancelButtonText" = "Cancel";
Binary file removed PYSearch/PYSearch.bundle/en.lproj/Root.strings
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions PYSearch/PYSearch.bundle/zh-Hant.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"PYSearchSearchPlaceholderText" = "搜索內容";
"PYSearchHotSearchText" = "熱門搜索";
"PYSearchSearchHistoryText" = "搜索歷史";
"PYSearchEmptySearchHistoryText" = "清空搜索歷史";

"PYSearchEmptyButtonText" = "清空";
"PYSearchEmptySearchHistoryLogText" = "清空搜索歷史";
"PYSearchCancelButtonText" = "取消";
15 changes: 11 additions & 4 deletions PYSearch/PYSearchConst.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import <UIKit/UIKit.h>
#import "UIView+PYSearchExtension.h"
#import "UIColor+PYSearchExtension.h"
#import "NSString+PYSearchExtension.h"

#define PYSEARCH_MARGIN 10 // 默认边距
#define PYSEARCH_BACKGROUND_COLOR PYSEARCH_COLOR(255, 255, 255) // tableView背景颜色
Expand Down Expand Up @@ -35,7 +36,13 @@
#define PYSEARCH_SEARCH_HISTORY_IMAGE [UIImage imageNamed:@"PYSearch.bundle/search_history"] // 搜索历史Cell的图片
#define PYSEARCH_SEARCH_SUGGESTION_IMAGE [UIImage imageNamed:@"PYSearch.bundle/search"] // 搜索建议时,Cell的图片

UIKIT_EXTERN NSString *const PYSearchPlaceholderText; // 搜索框的占位符 默认为 @"搜索内容"
UIKIT_EXTERN NSString *const PYHotSearchText; // 热门搜索文本 默认为 @"热门搜索"
UIKIT_EXTERN NSString *const PYSearchHistoryText; // 搜索历史文本 默认为 @"搜索历史"
UIKIT_EXTERN NSString *const PYEmptySearchHistoryText; // 清空搜索历史文本 默认为 @"清空搜索历史"
UIKIT_EXTERN NSString *const PYSearchSearchPlaceholderText; // 搜索框的占位符 默认为 @"搜索内容"
UIKIT_EXTERN NSString *const PYSearchHotSearchText; // 热门搜索文本 默认为 @"热门搜索"
UIKIT_EXTERN NSString *const PYSearchSearchHistoryText; // 搜索历史文本 默认为 @"搜索历史"
UIKIT_EXTERN NSString *const PYSearchEmptySearchHistoryText; // 清空搜索历史文本 默认为 @"清空搜索历史"

UIKIT_EXTERN NSString *const PYSearchEmptyButtonText; // 清空按钮文本,默认为 @"清空"
UIKIT_EXTERN NSString *const PYSearchEmptySearchHistoryLogText; // 清空搜索历史打印文本,默认为 @"清空搜索历史"
UIKIT_EXTERN NSString *const PYSearchCancelButtonText; // 取消按钮位文本,默认为 @"取消"


20 changes: 15 additions & 5 deletions PYSearch/PYSearchConst.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
//
// 代码地址: https://github.com/iphone5solo/PYSearch
// 代码地址: http://www.code4app.com/thread-11175-1-1.html
// Created by CoderKo1o.
// Copyright © 2016年 iphone5solo. All rights reserved.
//

#import "PYSearchConst.h"
#import <UIKit/UIKit.h>

NSString *const PYSearchPlaceholderText = @"搜索内容"; // 搜索框的占位符 默认为 @"搜索内容"
NSString *const PYHotSearchText = @"热门搜索"; // 热门搜索文本 默认为 @"热门搜索"
NSString *const PYSearchHistoryText = @"搜索历史"; // 搜索历史文本 默认为 @"搜索历史"
NSString *const PYEmptySearchHistoryText = @"清空搜索历史"; // 清空搜索历史文本 默认为 @"清空搜索历史"
NSString *const PYSearchSearchPlaceholderText = @"PYSearchSearchPlaceholderText";
NSString *const PYSearchHotSearchText = @"PYSearchHotSearchText";
NSString *const PYSearchSearchHistoryText = @"PYSearchSearchHistoryText";
NSString *const PYSearchEmptySearchHistoryText = @"PYSearchEmptySearchHistoryText";

NSString *const PYSearchEmptyButtonText = @"PYSearchEmptyButtonText";
NSString *const PYSearchEmptySearchHistoryLogText = @"PYSearchEmptySearchHistoryLogText";
NSString *const PYSearchCancelButtonText = @"PYSearchCancelButtonText";
16 changes: 8 additions & 8 deletions PYSearch/PYSearchViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ - (UIButton *)emptyButton
UIButton *emptyButton = [[UIButton alloc] init];
emptyButton.titleLabel.font = self.searchHistoryHeader.font;
[emptyButton setTitleColor:PYTextColor forState:UIControlStateNormal];
[emptyButton setTitle:@"清空" forState:UIControlStateNormal];
[emptyButton setTitle:[PYSearchEmptyButtonText py_localizedString] forState:UIControlStateNormal];
[emptyButton setImage:[UIImage imageNamed:@"PYSearch.bundle/empty"] forState:UIControlStateNormal];
[emptyButton addTarget:self action:@selector(emptySearchHistoryDidClick) forControlEvents:UIControlEventTouchUpInside];
[emptyButton sizeToFit];
Expand Down Expand Up @@ -167,7 +167,7 @@ - (UIView *)searchHistoryTagsContentView
- (UILabel *)searchHistoryHeader
{
if (!_searchHistoryHeader) {
UILabel *titleLabel = [self setupTitleLabel:PYSearchHistoryText];
UILabel *titleLabel = [self setupTitleLabel:[PYSearchSearchHistoryText py_localizedString]];
[self.headerContentView addSubview:titleLabel];
_searchHistoryHeader = titleLabel;
}
Expand Down Expand Up @@ -233,7 +233,7 @@ - (void)setup
self.view.backgroundColor = [UIColor whiteColor];
self.baseSearchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancelDidClick)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[PYSearchCancelButtonText py_localizedString] style:UIBarButtonItemStyleDone target:self action:@selector(cancelDidClick)];

/**
* 设置一些默认设置
Expand Down Expand Up @@ -263,7 +263,7 @@ - (void)setup
titleView.py_height = 30;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds];
searchBar.py_width -= PYSEARCH_MARGIN * 1.5;
searchBar.placeholder = PYSearchPlaceholderText;
searchBar.placeholder = [PYSearchSearchPlaceholderText py_localizedString];
searchBar.backgroundImage = [UIImage imageNamed:@"PYSearch.bundle/clearImage"];
searchBar.delegate = self;
[titleView addSubview:searchBar];
Expand All @@ -277,7 +277,7 @@ - (void)setup
contentView.py_x = PYSEARCH_MARGIN * 1.5;
contentView.py_width = PYScreenW - contentView.py_x * 2;
[headerView addSubview:contentView];
UILabel *titleLabel = [self setupTitleLabel:PYHotSearchText];
UILabel *titleLabel = [self setupTitleLabel:[PYSearchHotSearchText py_localizedString]];
self.hotSearchHeader = titleLabel;
[contentView addSubview:titleLabel];
// 创建热门搜索标签容器
Expand All @@ -296,7 +296,7 @@ - (void)setup
emptySearchHistoryLabel.textColor = [UIColor darkGrayColor];
emptySearchHistoryLabel.font = [UIFont systemFontOfSize:13];
emptySearchHistoryLabel.userInteractionEnabled = YES;
emptySearchHistoryLabel.text = PYEmptySearchHistoryText;
emptySearchHistoryLabel.text = [PYSearchEmptySearchHistoryText py_localizedString];
emptySearchHistoryLabel.textAlignment = NSTextAlignmentCenter;
emptySearchHistoryLabel.py_height = 30;
[emptySearchHistoryLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(emptySearchHistoryDidClick)]];
Expand Down Expand Up @@ -816,7 +816,7 @@ - (void)emptySearchHistoryDidClick
// 更新
self.searchHistoryStyle = self.searchHistoryStyle;
}
PYSEARCH_LOG(@"清空历史记录");
PYSEARCH_LOG(@"%@", [PYSearchEmptySearchHistoryLogText py_localizedString]);
}

/** 选中标签 */
Expand Down Expand Up @@ -1062,7 +1062,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.showSearchHistory && self.searchHistories.count && self.searchHistoryStyle == PYSearchHistoryStyleCell ? PYSearchHistoryText : nil;
return self.showSearchHistory && self.searchHistories.count && self.searchHistoryStyle == PYSearchHistoryStyleCell ? [PYSearchSearchHistoryText py_localizedString] : nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Expand Down
8 changes: 8 additions & 0 deletions PYSearchExample/PYSearchExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
6B1D94F31E100E41009C5F05 /* NSString+PYSearchExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B1D94F21E100E41009C5F05 /* NSString+PYSearchExtension.m */; };
6B3EB2111DC498370080DF86 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B3EB2101DC498370080DF86 /* AppDelegate.m */; };
6B3EB21F1DC498410080DF86 /* PYSearch.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 6B3EB2131DC498410080DF86 /* PYSearch.bundle */; };
6B3EB2201DC498410080DF86 /* PYSearchConst.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B3EB2161DC498410080DF86 /* PYSearchConst.m */; };
Expand Down Expand Up @@ -41,6 +42,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
6B1D94F11E100E41009C5F05 /* NSString+PYSearchExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+PYSearchExtension.h"; sourceTree = "<group>"; };
6B1D94F21E100E41009C5F05 /* NSString+PYSearchExtension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+PYSearchExtension.m"; sourceTree = "<group>"; };
6B3EB20F1DC498370080DF86 /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
6B3EB2101DC498370080DF86 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
6B3EB2131DC498410080DF86 /* PYSearch.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = PYSearch.bundle; sourceTree = "<group>"; };
Expand Down Expand Up @@ -112,6 +115,8 @@
6B3EB21C1DC498410080DF86 /* UIColor+PYSearchExtension.m */,
6B3EB21D1DC498410080DF86 /* UIView+PYSearchExtension.h */,
6B3EB21E1DC498410080DF86 /* UIView+PYSearchExtension.m */,
6B1D94F11E100E41009C5F05 /* NSString+PYSearchExtension.h */,
6B1D94F21E100E41009C5F05 /* NSString+PYSearchExtension.m */,
);
name = PYSearch;
path = ../../PYSearch;
Expand Down Expand Up @@ -313,6 +318,7 @@
files = (
6B3EB2201DC498410080DF86 /* PYSearchConst.m in Sources */,
6B3EB2241DC498410080DF86 /* UIView+PYSearchExtension.m in Sources */,
6B1D94F31E100E41009C5F05 /* NSString+PYSearchExtension.m in Sources */,
6B97179E1DC48FE800B2DAA1 /* PYSearchExampleController.m in Sources */,
6B3EB2211DC498410080DF86 /* PYSearchSuggestionViewController.m in Sources */,
6B3EB2221DC498410080DF86 /* PYSearchViewController.m in Sources */,
Expand Down Expand Up @@ -370,6 +376,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
Expand Down Expand Up @@ -414,6 +421,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 9b6cf21

Please sign in to comment.