Skip to content

Commit

Permalink
新增自行切换语言功能
Browse files Browse the repository at this point in the history
  • Loading branch information
longitachi committed Nov 4, 2017
1 parent e84e77d commit 018fdf0
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 32 deletions.
2 changes: 2 additions & 0 deletions PhotoBrowser/NSBundle+ZLPhotoBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

+ (instancetype)zlPhotoBrowserBundle;

+ (void)resetLanguage;

+ (NSString *)zlLocalizedStringForKey:(NSString *)key;

+ (NSString *)zlLocalizedStringForKey:(NSString *)key value:(NSString *)value;
Expand Down
74 changes: 51 additions & 23 deletions PhotoBrowser/NSBundle+ZLPhotoBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ @implementation NSBundle (ZLPhotoBrowser)

+ (instancetype)zlPhotoBrowserBundle
{
static NSBundle *refreshBundle = nil;
if (refreshBundle == nil) {
// 这里不使用mainBundle是为了适配pod 1.x和0.x
refreshBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[ZLPhotoActionSheet class]] pathForResource:@"ZLPhotoBrowser" ofType:@"bundle"]];
static NSBundle *photoBrowserBundle = nil;
if (photoBrowserBundle == nil) {
photoBrowserBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[ZLPhotoActionSheet class]] pathForResource:@"ZLPhotoBrowser" ofType:@"bundle"]];
}
return refreshBundle;
return photoBrowserBundle;
}

static NSBundle *bundle = nil;
+ (void)resetLanguage
{
bundle = nil;
}

+ (NSString *)zlLocalizedStringForKey:(NSString *)key
Expand All @@ -28,28 +33,51 @@ + (NSString *)zlLocalizedStringForKey:(NSString *)key

+ (NSString *)zlLocalizedStringForKey:(NSString *)key value:(NSString *)value
{
static NSBundle *bundle = nil;
if (bundle == nil) {
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 if ([language hasPrefix:@"ja"]) {
language = @"ja-US";
} else {
language = @"en";
}

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

+ (NSString *)getLanguage
{
ZLLanguageType type = [[[NSUserDefaults standardUserDefaults] valueForKey:ZLLanguageTypeKey] integerValue];

NSString *language = nil;
switch (type) {
case ZLLanguageSystem: {
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 if ([language hasPrefix:@"ja"]) {
language = @"ja-US";
} else {
language = @"en";
}
}
break;
case ZLLanguageChineseSimplified:
language = @"zh-Hans";
break;
case ZLLanguageChineseTraditional:
language = @"zh-Hant";
break;
case ZLLanguageEnglish:
language = @"en";
break;
case ZLLanguageJapanese:
language = @"ja-US";
break;
}
return language;
}

@end
15 changes: 15 additions & 0 deletions PhotoBrowser/ZLDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@

//自定义图片名称存于plist中的key
#define ZLCustomImageNames @"ZLCustomImageNames"
//设置框架语言的key
#define ZLLanguageTypeKey @"ZLLanguageTypeKey"

////////ZLShowBigImgViewController
#define kItemMargin 40
Expand All @@ -79,6 +81,19 @@
#define ClippingRatioValue2 @"value2"
#define ClippingRatioTitleFormat @"titleFormat"

typedef NS_ENUM(NSUInteger, ZLLanguageType) {
//跟随系统语言,默认
ZLLanguageSystem,
//中文简体
ZLLanguageChineseSimplified,
//中文繁体
ZLLanguageChineseTraditional,
//英文
ZLLanguageEnglish,
//日文
ZLLanguageJapanese,
};

static inline void SetViewWidth (UIView *view, CGFloat width) {
CGRect frame = view.frame;
frame.size.width = width;
Expand Down
5 changes: 5 additions & 0 deletions PhotoBrowser/ZLPhotoActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, assign) BOOL shouldAnialysisAsset;

/**
框架语言,默认 ZLLanguageSystem (跟随系统语言)
仅切换框架内定义的语言,由于相册名字根据系统语言进行获取,所以相册列表界面为当前系统语言
*/
@property (nonatomic, assign) ZLLanguageType languageType;

/**
选择照片回调,回调解析好的图片、对应的asset对象、是否原图
Expand Down
10 changes: 10 additions & 0 deletions PhotoBrowser/ZLPhotoActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "ToastUtils.h"
#import "ZLEditViewController.h"
#import "ZLEditVideoController.h"
#import "NSBundle+ZLPhotoBrowser.h"

#define kBaseViewHeight (self.maxPreviewCount ? 300 : 142)

Expand Down Expand Up @@ -94,6 +95,7 @@ - (UILabel *)placeholderLabel
return _placeholderLabel;
}

#pragma mark - setter
- (void)setArrSelectedAssets:(NSMutableArray<PHAsset *> *)arrSelectedAssets
{
_arrSelectedAssets = arrSelectedAssets;
Expand Down Expand Up @@ -125,6 +127,13 @@ - (void)setCustomImageNames:(NSArray<NSString *> *)customImageNames
[[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)setLanguageType:(ZLLanguageType)languageType
{
[[NSUserDefaults standardUserDefaults] setValue:@(languageType) forKey:ZLLanguageTypeKey];
[[NSUserDefaults standardUserDefaults] synchronize];
[NSBundle resetLanguage];
}

- (instancetype)init
{
self = [[kZLPhotoBrowserBundle loadNibNamed:@"ZLPhotoActionSheet" owner:self options:nil] lastObject];
Expand Down Expand Up @@ -163,6 +172,7 @@ - (instancetype)init
self.shouldAnialysisAsset = YES;
self.selectedMaskColor = [UIColor blackColor];
self.bottomBtnsNormalTitleColor = kBottomBtnsNormalTitleColor;
self.languageType = ZLLanguageSystem;
if (![self judgeIsHavePhotoAblumAuthority]) {
//注册实施监听相册变化
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
Expand Down
2 changes: 1 addition & 1 deletion PhotoBrowser/ZLPhotoBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ - (void)viewDidLoad {
self.title = GetLocalLanguageTextValue(ZLPhotoBrowserPhotoText);

self.tableView.tableFooterView = [[UIView alloc] init];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:GetLocalLanguageTextValue(ZLPhotoBrowserBackText) style:UIBarButtonItemStylePlain target:nil action:nil];
// self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:GetLocalLanguageTextValue(ZLPhotoBrowserBackText) style:UIBarButtonItemStylePlain target:nil action:nil];
[self initNavBtn];

if (@available(iOS 11.0, *)) {
Expand Down
3 changes: 3 additions & 0 deletions PhotoBrowser/ZLThumbnailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ - (void)setupBottomView
- (void)initNavBtn
{
ZLImageNavigationController *nav = (ZLImageNavigationController *)self.navigationController;

nav.viewControllers.firstObject.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:GetLocalLanguageTextValue(ZLPhotoBrowserBackText) style:UIBarButtonItemStylePlain target:nil action:nil];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat width = GetMatchValue(GetLocalLanguageTextValue(ZLPhotoBrowserCancelText), 16, YES, 44);
btn.frame = CGRectMake(0, 0, width, 44);
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
- [x] 混合选择image、gif、livePhoto、video
- [x] 在线下载iCloud照片
- [x] 控制选择video最大时长
- [x] 多语言国际化(中文简/繁、英文、日文)
- [x] 多语言国际化(中文简/繁、英文、日文,可设置跟随系统和自行切换)
- [x] 相册内拍照按钮实时显示镜头捕捉画面
- [x] 已选择图片遮罩层标记
- [x] 预览已选择照片
Expand All @@ -44,8 +44,9 @@
### 更新日志
```
● 2.5.0.1: 提供逐个解析图片api,方便`shouldAnialysisAsset`为`NO`时的使用; 提供控制是否可以选择原图参数;
● 2.5.0: 新增选择后是否自动解析图片参数`shouldAnialysisAsset`(针对需要选择大量图片的功能,框架一次解析大量图片时,会导致内存瞬间大幅增高,建议此时置该参数为NO,然后拿到asset后自行逐个解析); 修改图片压缩方式,确保原图尺寸不变
● 2.5.0.2: 新增自行切换框架语言api; 编辑图片界面当只有一个比例且为custom或1:1状态下隐藏比例切换工具条;
● 2.5.0.1: 提供逐个解析图片api,方便 shouldAnialysisAsset 为 NO 时的使用; 提供控制是否可以选择原图参数;
● 2.5.0: 新增选择后是否自动解析图片参数 shouldAnialysisAsset (针对需要选择大量图片的功能,框架一次解析大量图片时,会导致内存瞬间大幅增高,建议此时置该参数为NO,然后拿到asset后自行逐个解析); 修改图片压缩方式,确保原图尺寸不变
● 2.4.9: 新增预览界面拖拽选择的功能; 支持开发者使用自定义图片资源; 开放导航标题颜色、底部工具栏背景色、底部按钮可交互与不可交互标题颜色的设置api;
● 2.4.6: 新增网络图片长按保存至相册功能;
● 2.4.3: 适配iPhone X,优化初次启动进入相册速度,预览网络图片可设置是否显示底部工具条及导航右侧按钮;
Expand Down
2 changes: 1 addition & 1 deletion ZLPhotoBrowser.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ZLPhotoBrowser'
s.version = '2.5.0.1'
s.version = '2.5.0.2'
s.summary = 'A simple way to multiselect photos from ablum, force touch to preview photo, support portrait and landscape, edit photo, multiple languages(Chinese,English,Japanese)'
s.homepage = 'https://github.com/longitachi/ZLPhotoBrowser'
s.license = 'MIT'
Expand Down
Loading

0 comments on commit 018fdf0

Please sign in to comment.