<commit_before>/*
 * Copyright 2011 Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 */

#include "meegolocale.h"

#include <QDate>
#include <QDateTime>
#include <QTime>
#include <QLocale>

#include <unicode/unistr.h>
#include <unicode/locid.h>
#include <unicode/coll.h>
#include <unicode/tblcoll.h>
#include <unicode/uchar.h>
#include <unicode/ulocdata.h>
#include <unicode/ustring.h>
#include <unicode/uchriter.h>

#include <QDebug>


namespace
{
    QString toQString(const icu::UnicodeString & str)
    {
        const UChar * buf = str.getBuffer();
        return QString(reinterpret_cast<const QChar *>(buf), str.length());
    }

    icu::UnicodeString toUnicodeString(const QString & str)
    {
        const ushort *strShort = str.utf16();
        return UnicodeString(static_cast<const UChar *>(strShort));
    }


    enum CollationTypes {
        Default = 0,
        PhoneBook,
        Pinyin,
        Traditional,
        Stroke,
        Direct
    };


    bool compare (const QString & lStr, const QString & rStr, icu::Collator & coll)
    {
        UnicodeString lUniStr = toUnicodeString(lStr);
        UnicodeString rUniStr = toUnicodeString(rStr);

        icu::Collator::EComparisonResult result = coll.compare(lUniStr, rUniStr);

        switch (result) {
          case icu::Collator::LESS :
              return -1;
              break;
          case icu::Collator::GREATER :
              return 1;
              break;
          default :
              return 0;
        };
    }


    QLocale::Country country()
    {
        return QLocale::system().country();
    }


    icu::Collator * createCollator (int collType, QString locale, QLocale::Country country)
    {
        QString collation;

        switch (collType) {
          case PhoneBook:
              collation = "@collation=phonebook";
              break;
          case Pinyin:
              collation = "@collation=pinyin";
              break;
          case Traditional:
              collation = "@collation=traditional";
              break;
          case Stroke:
              collation = "@collation=stroke";
              break;
          case Direct:
              collation = "@collation=direct";
              break;
          default:
              collation = "@collation=default";
        }

        const char *name = (locale + collation).toLatin1().constData();
        icu::Locale localeName = icu::Locale(name);

        UErrorCode status = U_ZERO_ERROR;
        icu::Collator * pColl = icu::Collator::createInstance(localeName, status);
        
        if (U_SUCCESS(status) &&
            ((country == QLocale::DemocraticRepublicOfKorea) ||
             (country == QLocale::RepublicOfKorea) ||
             (country == QLocale::Japan))) {

            //ASCII characters should be sorted after
            //non-ASCII characters for some languages
            UnicodeString rules = ((icu::RuleBasedCollator *)pColl)->getRules();
            rules += "< a,A< b,B< c,C< d,D< e,E< f,F< g,G< h,H< i,I< j,J < k,K"
                "< l,L< m,M< n,N< o,O< p,P< q,Q< r,R< s,S< t,T < u,U< v,V"
                "< w,W< x,X< y,Y< z,Z";

            delete pColl;
            pColl = new icu::RuleBasedCollator(rules, status);
        }

        if (!U_SUCCESS(status)) {
            delete pColl;
            pColl = 0;
        }
        return pColl;
    }
    
} // anonymous namespace


namespace meego
{

    Locale::Locale(QObject *parent)
        : QObject(parent),
          mpQLocale(new QLocale()),
          mDateFormat(defaultDateFormat()),
          mTimeFormat(defaultTimeFormat()),
          mFirstDayOfWeek(defaultFirstDayOfWeek()),
          mDecimalPoint(defaultDecimalPoint()),
          mpDefaultCollator(0),
          mpPhoneBookCollator(0)
    {
        mLocale = QLocale::system().name();
    }

    Locale::DateFormat Locale::defaultDateFormat() const
    {
        // read DateFormat
        //: this string defines default order for dates
        //: choose one of "dmy", "ymd", or "mdy" (m = month, d = date, y = year)
        QString dateFormat = tr("mdy", "dateformat");

        if (dateFormat == "dmy") {
            return DateFormatDMY;
        }
        else if (dateFormat == "ymd") {
            return DateFormatYMD;
        }
        else {
            return DateFormatMDY;
        }
    }

    Locale::TimeFormat Locale::defaultTimeFormat() const
    {
        // read 24 hrs flag TimeFormat
        //: set to 1 for default 24-hour time format (0 for 12-hour)
        QString time24 = tr("0", "timeformat");
        if (time24 == "1") {
            return TimeFormat24;
        }
        else {
            return TimeFormat12;
        }
    }

    Locale::DayOfWeek Locale::defaultFirstDayOfWeek() const
    {
        // read DayOfWeek
        //: this number sets default first day of week
        //: set to 1 for Monday, 2 for Tuesday, etc, and 7 for Sunday
        QString day = tr("1", "firstday");

        int dow = 1;
        if (1 == day.length()) {<commit_msg>[meegolocale] Sunday should still be default first day of week in en_US<commit_after>/*
 * Copyright 2011 Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 */

#include "meegolocale.h"

#include <QDate>
#include <QDateTime>
#include <QTime>
#include <QLocale>

#include <unicode/unistr.h>
#include <unicode/locid.h>
#include <unicode/coll.h>
#include <unicode/tblcoll.h>
#include <unicode/uchar.h>
#include <unicode/ulocdata.h>
#include <unicode/ustring.h>
#include <unicode/uchriter.h>

#include <QDebug>


namespace
{
    QString toQString(const icu::UnicodeString & str)
    {
        const UChar * buf = str.getBuffer();
        return QString(reinterpret_cast<const QChar *>(buf), str.length());
    }

    icu::UnicodeString toUnicodeString(const QString & str)
    {
        const ushort *strShort = str.utf16();
        return UnicodeString(static_cast<const UChar *>(strShort));
    }


    enum CollationTypes {
        Default = 0,
        PhoneBook,
        Pinyin,
        Traditional,
        Stroke,
        Direct
    };


    bool compare (const QString & lStr, const QString & rStr, icu::Collator & coll)
    {
        UnicodeString lUniStr = toUnicodeString(lStr);
        UnicodeString rUniStr = toUnicodeString(rStr);

        icu::Collator::EComparisonResult result = coll.compare(lUniStr, rUniStr);

        switch (result) {
          case icu::Collator::LESS :
              return -1;
              break;
          case icu::Collator::GREATER :
              return 1;
              break;
          default :
              return 0;
        };
    }


    QLocale::Country country()
    {
        return QLocale::system().country();
    }


    icu::Collator * createCollator (int collType, QString locale, QLocale::Country country)
    {
        QString collation;

        switch (collType) {
          case PhoneBook:
              collation = "@collation=phonebook";
              break;
          case Pinyin:
              collation = "@collation=pinyin";
              break;
          case Traditional:
              collation = "@collation=traditional";
              break;
          case Stroke:
              collation = "@collation=stroke";
              break;
          case Direct:
              collation = "@collation=direct";
              break;
          default:
              collation = "@collation=default";
        }

        const char *name = (locale + collation).toLatin1().constData();
        icu::Locale localeName = icu::Locale(name);

        UErrorCode status = U_ZERO_ERROR;
        icu::Collator * pColl = icu::Collator::createInstance(localeName, status);
        
        if (U_SUCCESS(status) &&
            ((country == QLocale::DemocraticRepublicOfKorea) ||
             (country == QLocale::RepublicOfKorea) ||
             (country == QLocale::Japan))) {

            //ASCII characters should be sorted after
            //non-ASCII characters for some languages
            UnicodeString rules = ((icu::RuleBasedCollator *)pColl)->getRules();
            rules += "< a,A< b,B< c,C< d,D< e,E< f,F< g,G< h,H< i,I< j,J < k,K"
                "< l,L< m,M< n,N< o,O< p,P< q,Q< r,R< s,S< t,T < u,U< v,V"
                "< w,W< x,X< y,Y< z,Z";

            delete pColl;
            pColl = new icu::RuleBasedCollator(rules, status);
        }

        if (!U_SUCCESS(status)) {
            delete pColl;
            pColl = 0;
        }
        return pColl;
    }
    
} // anonymous namespace


namespace meego
{

    Locale::Locale(QObject *parent)
        : QObject(parent),
          mpQLocale(new QLocale()),
          mDateFormat(defaultDateFormat()),
          mTimeFormat(defaultTimeFormat()),
          mFirstDayOfWeek(defaultFirstDayOfWeek()),
          mDecimalPoint(defaultDecimalPoint()),
          mpDefaultCollator(0),
          mpPhoneBookCollator(0)
    {
        mLocale = QLocale::system().name();
    }

    Locale::DateFormat Locale::defaultDateFormat() const
    {
        // read DateFormat
        //: this string defines default order for dates
        //: choose one of "dmy", "ymd", or "mdy" (m = month, d = date, y = year)
        QString dateFormat = tr("mdy", "dateformat");

        if (dateFormat == "dmy") {
            return DateFormatDMY;
        }
        else if (dateFormat == "ymd") {
            return DateFormatYMD;
        }
        else {
            return DateFormatMDY;
        }
    }

    Locale::TimeFormat Locale::defaultTimeFormat() const
    {
        // read 24 hrs flag TimeFormat
        //: set to 1 for default 24-hour time format (0 for 12-hour)
        QString time24 = tr("0", "timeformat");
        if (time24 == "1") {
            return TimeFormat24;
        }
        else {
            return TimeFormat12;
        }
    }

    Locale::DayOfWeek Locale::defaultFirstDayOfWeek() const
    {
        // read DayOfWeek
        //: this number sets default first day of week
        //: set to 1 for Monday, 2 for Tuesday, etc, and 7 for Sunday
        QString day = tr("7", "firstday");

        int dow = 7;
        if (1 == day.length()) {
{'fraction_non_alphanumeric': 0.11966961198616981, 'fraction_numerical': 0.008067614291202459, 'mean_word_length': 1.9599204093234792, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3070875', 'n_tokens_mistral': 3208, 'n_tokens_neox': 3090, 'n_words': 767}