From f65ef462e509e13e041234c1ee3c23b93c6df91a Mon Sep 17 00:00:00 2001 From: Sergey Date: Mon, 17 Mar 2014 22:34:21 +0400 Subject: [PATCH 1/2] getCurrentLanguageCode() for all platforms return iso 639-1 language code iPhone template improvements(for game center support) --- cocos/2d/platform/CCApplicationProtocol.h | 8 ++++++++ cocos/2d/platform/android/CCApplication.cpp | 8 ++++++++ cocos/2d/platform/android/CCApplication.h | 6 ++++++ cocos/2d/platform/ios/CCApplication.h | 6 ++++++ cocos/2d/platform/ios/CCApplication.mm | 15 +++++++++++++++ cocos/2d/platform/linux/CCApplication.cpp | 14 ++++++++++++++ cocos/2d/platform/linux/CCApplication.h | 7 +++++++ cocos/2d/platform/mac/CCApplication.h | 6 ++++++ cocos/2d/platform/mac/CCApplication.mm | 15 +++++++++++++++ cocos/2d/platform/win32/CCApplication.cpp | 10 ++++++++++ cocos/2d/platform/win32/CCApplication.h | 2 ++ .../proj.ios_mac/ios/AppController.h | 3 ++- .../proj.ios_mac/ios/AppController.mm | 10 +++++----- 13 files changed, 104 insertions(+), 6 deletions(-) mode change 100644 => 100755 cocos/2d/platform/win32/CCApplication.cpp mode change 100644 => 100755 cocos/2d/platform/win32/CCApplication.h diff --git a/cocos/2d/platform/CCApplicationProtocol.h b/cocos/2d/platform/CCApplicationProtocol.h index a8de2967be..7c47c46e81 100644 --- a/cocos/2d/platform/CCApplicationProtocol.h +++ b/cocos/2d/platform/CCApplicationProtocol.h @@ -100,6 +100,14 @@ public: */ virtual LanguageType getCurrentLanguage() = 0; + /** + @brief Get current language iso 639-1 code + @return Current language iso 639-1 code + * @js NA + * @lua NA + */ + virtual const char * getCurrentLanguageCode() = 0; + /** @brief Get target platform * @js NA diff --git a/cocos/2d/platform/android/CCApplication.cpp b/cocos/2d/platform/android/CCApplication.cpp index 65990b7c08..d940c6890e 100644 --- a/cocos/2d/platform/android/CCApplication.cpp +++ b/cocos/2d/platform/android/CCApplication.cpp @@ -86,6 +86,14 @@ Application* Application::sharedApplication() return Application::getInstance(); } +const char * Application::getCurrentLanguageCode() +{ + static char code[3]={0}; + strncpy(code,getCurrentLanguageJNI().c_str(),2); + code[2]='\0'; + return code; +} + LanguageType Application::getCurrentLanguage() { std::string languageName = getCurrentLanguageJNI(); diff --git a/cocos/2d/platform/android/CCApplication.h b/cocos/2d/platform/android/CCApplication.h index c3d3ce1a5d..4d664cc9f3 100644 --- a/cocos/2d/platform/android/CCApplication.h +++ b/cocos/2d/platform/android/CCApplication.h @@ -73,6 +73,12 @@ public: */ virtual LanguageType getCurrentLanguage(); + /** + @brief Get current language iso 639-1 code + @return Current language iso 639-1 code + */ + virtual const char * getCurrentLanguageCode(); + /** @brief Get target platform */ diff --git a/cocos/2d/platform/ios/CCApplication.h b/cocos/2d/platform/ios/CCApplication.h index b2a9ad8d95..6d7fe1aafd 100644 --- a/cocos/2d/platform/ios/CCApplication.h +++ b/cocos/2d/platform/ios/CCApplication.h @@ -75,6 +75,12 @@ public: */ virtual LanguageType getCurrentLanguage(); + /** + @brief Get current language iso 639-1 code + @return Current language iso 639-1 code + */ + virtual const char * getCurrentLanguageCode(); + /** @brief Get target platform */ diff --git a/cocos/2d/platform/ios/CCApplication.mm b/cocos/2d/platform/ios/CCApplication.mm index 052cfa0665..374037b1b8 100644 --- a/cocos/2d/platform/ios/CCApplication.mm +++ b/cocos/2d/platform/ios/CCApplication.mm @@ -78,6 +78,21 @@ Application* Application::sharedApplication() return Application::getInstance(); } +const char * Application::getCurrentLanguageCode() +{ + static char code[3]={0}; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; + NSString *currentLanguage = [languages objectAtIndex:0]; + + // get the current language code.(such as English is "en", Chinese is "zh" and so on) + NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage]; + NSString * languageCode = [temp objectForKey:NSLocaleLanguageCode]; + [languageCode getCString:code maxLength:2 encoding:NSASCIIStringEncoding]; + code[2]='\0'; + return code; +} + LanguageType Application::getCurrentLanguage() { // get the current language and country config diff --git a/cocos/2d/platform/linux/CCApplication.cpp b/cocos/2d/platform/linux/CCApplication.cpp index 9ea95ec06f..789d544bcb 100644 --- a/cocos/2d/platform/linux/CCApplication.cpp +++ b/cocos/2d/platform/linux/CCApplication.cpp @@ -151,6 +151,20 @@ Application* Application::sharedApplication() return Application::getInstance(); } +const char * Application::getCurrentLanguageCode() +{ + static char code[3]={0}; + char *pLanguageName = getenv("LANG"); + if (!pLanguageName) + return "en"; + strtok(pLanguageName, "_"); + if (!pLanguageName) + return "en"; + strncpy(code,pLanguageName,2); + code[2]='\0'; + return code; +} + LanguageType Application::getCurrentLanguage() { char *pLanguageName = getenv("LANG"); diff --git a/cocos/2d/platform/linux/CCApplication.h b/cocos/2d/platform/linux/CCApplication.h index e71e487f2f..b799c7a009 100644 --- a/cocos/2d/platform/linux/CCApplication.h +++ b/cocos/2d/platform/linux/CCApplication.h @@ -72,6 +72,13 @@ public: /* override functions */ virtual LanguageType getCurrentLanguage(); + /** + @brief Get current language iso 639-1 code + @return Current language iso 639-1 code + */ + virtual const char * getCurrentLanguageCode(); + + /** * Sets the Resource root path. * @deprecated Please use FileUtils::getInstance()->setSearchPaths() instead. diff --git a/cocos/2d/platform/mac/CCApplication.h b/cocos/2d/platform/mac/CCApplication.h index 5d1c9b5cd5..d85428a9a3 100644 --- a/cocos/2d/platform/mac/CCApplication.h +++ b/cocos/2d/platform/mac/CCApplication.h @@ -80,6 +80,12 @@ public: */ virtual LanguageType getCurrentLanguage(); + /** + @brief Get current language iso 639-1 code + @return Current language iso 639-1 code + */ + virtual const char * getCurrentLanguageCode(); + /** @brief Get target platform */ diff --git a/cocos/2d/platform/mac/CCApplication.mm b/cocos/2d/platform/mac/CCApplication.mm index 43dd21583a..08f3cc55a1 100644 --- a/cocos/2d/platform/mac/CCApplication.mm +++ b/cocos/2d/platform/mac/CCApplication.mm @@ -134,6 +134,21 @@ Application* Application::sharedApplication() return Application::getInstance(); } +const char * Application::getCurrentLanguageCode() +{ + static char code[3]={0}; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; + NSString *currentLanguage = [languages objectAtIndex:0]; + + // get the current language code.(such as English is "en", Chinese is "zh" and so on) + NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage]; + NSString * languageCode = [temp objectForKey:NSLocaleLanguageCode]; + [languageCode getCString:code maxLength:2 encoding:NSASCIIStringEncoding]; + code[2]='\0'; + return code; +} + LanguageType Application::getCurrentLanguage() { // get the current language and country config diff --git a/cocos/2d/platform/win32/CCApplication.cpp b/cocos/2d/platform/win32/CCApplication.cpp old mode 100644 new mode 100755 index 05dd5ee4ac..6a9dc89a2f --- a/cocos/2d/platform/win32/CCApplication.cpp +++ b/cocos/2d/platform/win32/CCApplication.cpp @@ -190,6 +190,16 @@ LanguageType Application::getCurrentLanguage() return ret; } +const char * Application::getCurrentLanguageCode() +{ + LANGID lid = GetUserDefaultUILanguage(); + const LCID locale_id = MAKELCID(lid, SORT_DEFAULT); + static char code[3] = { 0 }; + GetLocaleInfoA(locale_id, LOCALE_SISO639LANGNAME, code, sizeof(code)); + code[2] = '\0'; + return code; +} + Application::Platform Application::getTargetPlatform() { return Platform::OS_WINDOWS; diff --git a/cocos/2d/platform/win32/CCApplication.h b/cocos/2d/platform/win32/CCApplication.h old mode 100644 new mode 100755 index c003280228..0a02e9d020 --- a/cocos/2d/platform/win32/CCApplication.h +++ b/cocos/2d/platform/win32/CCApplication.h @@ -67,6 +67,8 @@ public: /* override functions */ virtual void setAnimationInterval(double interval); virtual LanguageType getCurrentLanguage(); + + virtual const char * getCurrentLanguageCode(); /** @brief Get target platform diff --git a/templates/cpp-template-default/proj.ios_mac/ios/AppController.h b/templates/cpp-template-default/proj.ios_mac/ios/AppController.h index 0c73d6a13e..978e6e36c4 100644 --- a/templates/cpp-template-default/proj.ios_mac/ios/AppController.h +++ b/templates/cpp-template-default/proj.ios_mac/ios/AppController.h @@ -4,8 +4,9 @@ @interface AppController : NSObject { UIWindow *window; - RootViewController *viewController; } +@property(nonatomic, readonly) RootViewController* viewController; + @end diff --git a/templates/cpp-template-default/proj.ios_mac/ios/AppController.mm b/templates/cpp-template-default/proj.ios_mac/ios/AppController.mm index 38f7c3861a..e1e807c39d 100644 --- a/templates/cpp-template-default/proj.ios_mac/ios/AppController.mm +++ b/templates/cpp-template-default/proj.ios_mac/ios/AppController.mm @@ -53,20 +53,20 @@ static AppDelegate s_sharedApplication; numberOfSamples: 0]; // Use RootViewController manage CCEAGLView - viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; - viewController.wantsFullScreenLayout = YES; - viewController.view = eaglView; + _viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; + _viewController.wantsFullScreenLayout = YES; + _viewController.view = eaglView; // Set RootViewController to window if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) { // warning: addSubView doesn't work on iOS6 - [window addSubview: viewController.view]; + [window addSubview: _viewController.view]; } else { // use this method on ios6 - [window setRootViewController:viewController]; + [window setRootViewController:_viewController]; } [window makeKeyAndVisible]; From 13df593ae7f8b24db060351b8be2060543643f13 Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 19 Mar 2014 18:58:19 +0400 Subject: [PATCH 2/2] file attributes fixed --- cocos/2d/platform/win32/CCApplication.cpp | 0 cocos/2d/platform/win32/CCApplication.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 cocos/2d/platform/win32/CCApplication.cpp mode change 100755 => 100644 cocos/2d/platform/win32/CCApplication.h diff --git a/cocos/2d/platform/win32/CCApplication.cpp b/cocos/2d/platform/win32/CCApplication.cpp old mode 100755 new mode 100644 diff --git a/cocos/2d/platform/win32/CCApplication.h b/cocos/2d/platform/win32/CCApplication.h old mode 100755 new mode 100644