-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from pec1985/timob-8583
TIMOB-8583: Implemented Ti.Locale module and L()
- Loading branch information
Showing
16 changed files
with
305 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
|
||
#include "LocaleParser.h" | ||
|
||
static LocaleParser* _instance; | ||
|
||
LocaleParser::LocaleParser() { | ||
setLocale(QLocale().name()); | ||
} | ||
|
||
LocaleParser::~LocaleParser() { | ||
|
||
} | ||
|
||
QString LocaleParser::getString(QString value1, QString value2) | ||
{ | ||
if(stringsMap_.contains(value1)) | ||
return stringsMap_.value(value1); | ||
|
||
if(value2 != "undefined") | ||
return value2; | ||
|
||
return value1; | ||
} | ||
|
||
QString LocaleParser::getLocale() { | ||
return locale_; | ||
} | ||
|
||
QString LocaleParser::getLocaleLanguage() { | ||
return localeLanguage_; | ||
} | ||
|
||
QString LocaleParser::getLocaleCountry() { | ||
return localeCountry_; | ||
} | ||
|
||
void LocaleParser::setLocale(QString value) | ||
{ | ||
stringsMap_.empty(); | ||
localeLanguage_ = QString(value.left(2).toLower()); | ||
localeCountry_ = QString(value.right(2).toUpper()); | ||
locale_ = value; | ||
QString localeFile = QString("app/native/assets/i18n/"); | ||
localeFile.append(localeLanguage_); | ||
localeFile.append("/strings.xml"); | ||
|
||
bb::data::XmlDataAccess xda; | ||
QVariant xml = xda.load(localeFile, "resources/string"); | ||
if(xda.hasError()) return; | ||
// TODO: Handle error | ||
|
||
QList<QVariant> list = xml.toList(); | ||
for(int i = 0; i < list.length(); i++) { | ||
QVariant rawItem = list.at(i); | ||
QVariantMap item = rawItem.toMap(); | ||
stringsMap_.insert(item.value("name").toString(), item.value(".data").toString()); | ||
} | ||
|
||
} | ||
|
||
LocaleParser *LocaleParser::sharedInstance() | ||
{ | ||
if(_instance == NULL) | ||
_instance = new LocaleParser(); | ||
return _instance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
|
||
#ifndef LOCALEPARSER_H_ | ||
#define LOCALEPARSER_H_ | ||
|
||
#include <QLocale> | ||
#include <QObject> | ||
#include <QMap> | ||
#include <bb/data/XmlDataAccess> | ||
|
||
class LocaleParser { | ||
public: | ||
LocaleParser(); | ||
virtual ~LocaleParser(); | ||
static LocaleParser *sharedInstance(); | ||
QString getString(QString value1, QString value2); | ||
QString getLocaleLanguage(); | ||
QString getLocaleCountry(); | ||
QString getLocale(); | ||
void setLocale(QString value); | ||
private: | ||
QMap<QString, QString> stringsMap_; | ||
QString localeLanguage_; | ||
QString localeCountry_; | ||
QString locale_; | ||
}; | ||
|
||
#endif /* LOCALEPARSER_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
|
||
#include "TiLocaleObject.h" | ||
#include "TiGenericFunctionObject.h" | ||
#include <bb/data/XmlDataAccess> | ||
|
||
TiLocaleObject::TiLocaleObject() | ||
: TiProxy("Locale") | ||
{ | ||
} | ||
|
||
TiLocaleObject::~TiLocaleObject() | ||
{ | ||
} | ||
|
||
void TiLocaleObject::addObjectToParent(TiObject* parent) | ||
{ | ||
TiLocaleObject* obj = new TiLocaleObject; | ||
parent->addMember(obj); | ||
} | ||
|
||
void TiLocaleObject::onCreateStaticMembers() | ||
{ | ||
TiProxy::onCreateStaticMembers(); | ||
TiGenericFunctionObject::addGenericFunctionToParent(this, "getString", this, _getString); | ||
|
||
TiGenericFunctionObject::addGenericFunctionToParent(this, "getCurrentLanguage", this, _getCurrentLanguage); | ||
TiGenericFunctionObject::addGenericFunctionToParent(this, "getCurrentCountry", this, _getCurrentCountry); | ||
TiGenericFunctionObject::addGenericFunctionToParent(this, "getCurrentLocale", this, _getCurrentLocale); | ||
|
||
ADD_STATIC_TI_VALUE("currentLanguage", TiLocaleObject::_getCurrentLanguage(), this); | ||
ADD_STATIC_TI_VALUE("currentCountry", TiLocaleObject::_getCurrentCountry(), this); | ||
ADD_STATIC_TI_VALUE("currentLocale", TiLocaleObject::_getCurrentLocale(), this); | ||
} | ||
|
||
Handle<Value> TiLocaleObject::_getCurrentCountry() | ||
{ | ||
QString value = LocaleParser::sharedInstance()->getLocaleCountry(); | ||
Handle<String> result = String::New(value.toUtf8()); | ||
return result; | ||
} | ||
|
||
Handle<Value> TiLocaleObject::_getCurrentLocale() | ||
{ | ||
QString value = LocaleParser::sharedInstance()->getLocale(); | ||
Handle<String> result = String::New(value.toUtf8()); | ||
return result; | ||
} | ||
|
||
Handle<Value> TiLocaleObject::_getCurrentLanguage() | ||
{ | ||
QString value = LocaleParser::sharedInstance()->getLocaleLanguage(); | ||
Handle<String> result = String::New(value.toUtf8()); | ||
return result; | ||
} | ||
|
||
Handle<Value> TiLocaleObject::_getCurrentCountry(void*, TiObject*, const Arguments& args) { | ||
return TiLocaleObject::_getCurrentCountry(); | ||
} | ||
Handle<Value> TiLocaleObject::_getCurrentLocale(void*, TiObject*, const Arguments& args) | ||
{ | ||
return TiLocaleObject::_getCurrentLocale(); | ||
} | ||
Handle<Value> TiLocaleObject::_getCurrentLanguage(void*, TiObject*, const Arguments& args) | ||
{ | ||
return TiLocaleObject::_getCurrentLanguage(); | ||
} | ||
Handle<Value> TiLocaleObject::_getString(void*, TiObject*, const Arguments& args) | ||
{ | ||
Handle<String> arg1 = Handle<String>::Cast(args[0]); | ||
Handle<String> arg2 = Handle<String>::Cast(args[1]); | ||
|
||
String::Utf8Value arg1string(arg1); | ||
String::Utf8Value arg2string(arg2); | ||
|
||
QString value1 = QString(*arg1string); | ||
QString value2 = QString(*arg2string); | ||
QString returnedString; | ||
|
||
returnedString = LocaleParser::sharedInstance()->getString(value1, value2); | ||
|
||
Handle<String> result = String::New(returnedString.toUtf8()); | ||
return result; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
|
||
#ifndef TILOCALEOBJECT_H_ | ||
#define TILOCALEOBJECT_H_ | ||
|
||
#include "LocaleParser.h" | ||
|
||
#include "TiProxy.h" | ||
|
||
/* | ||
* TiLocaleObject | ||
* | ||
* Locale module containing stream utility methods. | ||
*/ | ||
class TiLocaleObject : public TiProxy | ||
{ | ||
public: | ||
static void addObjectToParent(TiObject* parent); | ||
static Handle<Value> _getString(void*, TiObject*, const Arguments& args); | ||
|
||
protected: | ||
virtual ~TiLocaleObject(); | ||
virtual void onCreateStaticMembers(); | ||
|
||
private: | ||
TiLocaleObject(); | ||
TiLocaleObject(const TiLocaleObject&); | ||
TiLocaleObject& operator=(const TiLocaleObject&); | ||
static Handle<Value> _getCurrentLanguage(void*, TiObject*, const Arguments& args); | ||
static Handle<Value> _getCurrentCountry(void*, TiObject*, const Arguments& args); | ||
static Handle<Value> _getCurrentLocale(void*, TiObject*, const Arguments& args); | ||
static Handle<Value> _getCurrentLanguage(); | ||
static Handle<Value> _getCurrentCountry(); | ||
static Handle<Value> _getCurrentLocale(); | ||
}; | ||
|
||
#endif /* TILOCALEOBJECT_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.