NatNum12: fix and add capitalization prefixes, tdf#115007 follow-up

Limit NatNum12 conversion only for the selected parts of the
date format (this bug – double calls of getNumberText – was hidden
by the space prefix " " and empty return values at the first calls,
resulting unchanged dates yet).

New prefixes: "capitalize", "upper" and "title" to handle optional
capitalization. (In Calc, it was not possible to format the result of
NatNum formatting, but some languages often need capitalization
or title case to format numbers and currencies.)

Thanks code clean up using enum WhichCasing to Eike Rathke.

Change-Id: I5fceb784930e6bc6d376116f5a42ad49cd248a54
Reviewed-on: https://gerrit.libreoffice.org/55681
Tested-by: Jenkins
Reviewed-by: Eike Rathke <erack@redhat.com>
This commit is contained in:
László Németh
2018-06-12 11:50:05 +02:00
committed by Eike Rathke
parent ec36da3788
commit 261ff0cdf0
6 changed files with 77 additions and 24 deletions

View File

@@ -25,7 +25,6 @@
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/i18n/XTransliteration.hpp>
#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
#include <com/sun/star/i18n/CharacterClassification.hpp>
#include <cppuhelper/implbase.hxx>
#include <rtl/ref.hxx>
@@ -81,7 +80,6 @@ private:
OUString makeNumberingIdentifier( sal_Int16 index );
/// @throws css::uno::RuntimeException
bool isScriptFlagEnabled(const OUString& aName );
mutable css::uno::Reference< css::i18n::XCharacterClassification > xCharClass;
};
}

View File

@@ -24,6 +24,7 @@
#include <com/sun/star/i18n/NativeNumberXmlAttributes.hpp>
#include <cppuhelper/implbase.hxx>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/i18n/CharacterClassification.hpp>
namespace i18npool {
@@ -76,6 +77,7 @@ public:
private:
css::lang::Locale aLocale;
bool useOffset;
mutable css::uno::Reference< css::i18n::XCharacterClassification > xCharClass;
};
}

View File

@@ -935,7 +935,8 @@ Calendar_gregorian::getDisplayStringImpl( sal_Int32 nCalendarDisplayCode, sal_In
}
aOUStr = OUString::createFromAscii(aStr);
}
if (nNativeNumberMode > 0) {
// NatNum12 used only for selected parts
if (nNativeNumberMode > 0 && nNativeNumberMode != 12) {
// For Japanese calendar, first year calls GAN, see bug 111668 for detail.
if (eraArray == gengou_eraArray && value == 1
&& (nCalendarDisplayCode == CalendarDisplayCode::SHORT_YEAR ||

View File

@@ -584,7 +584,6 @@ DefaultNumberingProvider::makeNumberingString( const Sequence<beans::PropertyVal
sal_Int16 tableSize = 0;
const sal_Unicode *table = nullptr; // initialize to avoid compiler warning
bool bRecycleSymbol = false;
bool bCapitalize = false;
OUString sNatNumParams;
Locale locale;
@@ -638,21 +637,18 @@ DefaultNumberingProvider::makeNumberingString( const Sequence<beans::PropertyVal
break;
case TEXT_NUMBER: // ordinal indicators (1st, 2nd, 3rd, ...)
natNum = NativeNumberMode::NATNUM12;
sNatNumParams = "ordinal-number";
sNatNumParams = "capitalize ordinal-number";
locale = aLocale;
bCapitalize = true;
break;
case TEXT_CARDINAL: // cardinal number names (One, Two, Three, ...)
natNum = NativeNumberMode::NATNUM12;
sNatNumParams = "cardinal";
sNatNumParams = "capitalize";
locale = aLocale;
bCapitalize = true;
break;
case TEXT_ORDINAL: // ordinal number names (First, Second, Third, ...)
natNum = NativeNumberMode::NATNUM12;
sNatNumParams = "ordinal";
sNatNumParams = "capitalize ordinal";
locale = aLocale;
bCapitalize = true;
break;
case ROMAN_UPPER:
result += toRoman( number );
@@ -913,17 +909,8 @@ DefaultNumberingProvider::makeNumberingString( const Sequence<beans::PropertyVal
if (natNum) {
rtl::Reference<NativeNumberSupplierService> xNatNum(new NativeNumberSupplierService);
OUString aNum = xNatNum->getNativeNumberStringParams(OUString::number(number), locale,
result += xNatNum->getNativeNumberStringParams(OUString::number(number), locale,
natNum, sNatNumParams);
if (bCapitalize)
{
if (!xCharClass.is())
xCharClass = CharacterClassification::create(m_xContext);
// capitalize first letter
result += xCharClass->toTitle(aNum, 0, 1, aLocale) + aNum.copy(1);
}
else
result += aNum;
} else if (tableSize) {
if ( number > tableSize && !bRecycleSymbol)
result += OUString::number( number);

View File

@@ -597,7 +597,7 @@ OUString getNumberText(const Locale& rLocale, const OUString& rNumberString,
= css::linguistic2::NumberText::create(comphelper::getProcessComponentContext());
OUString numbertext_prefix;
// default "cardinal" gets empty prefix
if (sNumberTextParams != "cardinal")
if (!sNumberTextParams.isEmpty() && sNumberTextParams != "cardinal")
numbertext_prefix = sNumberTextParams + " ";
// Several hundreds of headings could result typing lags because
// of the continuous update of the multiple number names during typing.
@@ -628,7 +628,63 @@ OUString NativeNumberSupplierService::getNativeNumberString(const OUString& aNum
return aNumberString;
if (nNativeNumberMode == NativeNumberMode::NATNUM12)
return getNumberText(rLocale, aNumberString, rNativeNumberParams);
{
// handle capitalization prefixes "capitalize", "upper" and "title"
enum WhichCasing
{
CAPITALIZE,
UPPER,
TITLE
};
struct CasingEntry
{
OUStringLiteral aLiteral;
WhichCasing eCasing;
};
static const CasingEntry Casings[] =
{
{ OUStringLiteral("capitalize"), CAPITALIZE },
{ OUStringLiteral("upper"), UPPER },
{ OUStringLiteral("title"), TITLE }
};
sal_Int32 nStripCase = 0;
size_t nCasing;
for (nCasing = 0; nCasing < SAL_N_ELEMENTS(Casings); ++nCasing)
{
if (rNativeNumberParams.startsWith( Casings[nCasing].aLiteral))
{
nStripCase = Casings[nCasing].aLiteral.size;
break;
}
}
if (nStripCase > 0 && (rNativeNumberParams.getLength() == nStripCase ||
rNativeNumberParams[nStripCase++] == ' '))
{
OUString aStr = getNumberText(rLocale, aNumberString, rNativeNumberParams.copy(nStripCase));
if (!xCharClass.is())
xCharClass = CharacterClassification::create(comphelper::getProcessComponentContext());
switch (Casings[nCasing].eCasing)
{
case CAPITALIZE:
return xCharClass->toTitle(aStr, 0, 1, aLocale) + aStr.copy(1);
case UPPER:
return xCharClass->toUpper(aStr, 0, aStr.getLength(), aLocale);
case TITLE:
return xCharClass->toTitle(aStr, 0, aStr.getLength(), aLocale);
}
}
else
{
return getNumberText(rLocale, aNumberString, rNativeNumberParams);
}
}
sal_Int16 langnum = getLanguageNumber(rLocale);
if (langnum == -1)

View File

@@ -1394,14 +1394,23 @@ void Test::testUserDefinedNumberFormats()
sCode = "[NatNum12 ordinal-number]0";
sExpected = "123rd";
checkPreviewString(aFormatter, sCode, 123, eLang, sExpected);
sCode = "[NatNum12 capitalize]0";
sExpected = "One hundred twenty-three";
checkPreviewString(aFormatter, sCode, 123, eLang, sExpected);
sCode = "[NatNum12 title ordinal]0";
sExpected = "One Thousand Two Hundred Thirty-Fourth";
checkPreviewString(aFormatter, sCode, 1234, eLang, sExpected);
sCode = "[NatNum12 upper ordinal-number]0";
sExpected = "12345TH";
checkPreviewString(aFormatter, sCode, 12345, eLang, sExpected);
sCode = "[NatNum12 D=ordinal-number]D\" of \"MMMM";
sExpected = "2nd of January";
checkPreviewString(aFormatter, sCode, M_PI, eLang, sExpected);
sCode = "[NatNum12 D=ordinal-number,YYYY=year]D\" of \"MMMM\", \"YYYY";
sExpected = "2nd of January, nineteen hundred";
checkPreviewString(aFormatter, sCode, M_PI, eLang, sExpected);
sCode = "[NatNum12 YYYY=year, D=ordinal]D\" of \"MMMM\", \"YYYY";
sExpected = "second of January, nineteen hundred";
sCode = "[NatNum12 YYYY=title year, D=capitalize ordinal]D\" of \"MMMM\", \"YYYY";
sExpected = "Second of January, Nineteen Hundred";
checkPreviewString(aFormatter, sCode, M_PI, eLang, sExpected);
#endif
}