update emoji autocorrect entries from po-files
[LibreOffice.git] / i18npool / source / ordinalsuffix / ordinalsuffix.cxx
blob17546ef9ee15cfee9d52dba3f9601a85c5e3eef5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/uno/XComponentContext.hpp>
21 #include <boost/scoped_ptr.hpp>
22 #include <i18nlangtag/languagetag.hxx>
23 #include <i18nlangtag/languagetagicu.hxx>
24 #include <sal/log.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <string.h>
27 #include "ordinalsuffix.hxx"
29 #include <unicode/rbnf.h>
30 #include <unicode/normlzr.h>
32 using namespace ::com::sun::star::i18n;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star;
36 namespace com { namespace sun { namespace star { namespace i18n {
39 OrdinalSuffixService::OrdinalSuffixService()
43 OrdinalSuffixService::~OrdinalSuffixService()
47 namespace
49 OUString mungeUnicodeStringToOUString(const icu::UnicodeString &rIn, UErrorCode &rCode)
51 // Apply NFKC normalization to get normal letters
52 icu::UnicodeString normalized;
53 icu::Normalizer::normalize(rIn, UNORM_NFKC, 0, normalized, rCode);
54 // Convert the normalized UnicodeString to OUString
55 OUString sRet = (U_SUCCESS(rCode))
56 ? OUString(reinterpret_cast<const sal_Unicode *>(normalized.getBuffer()), normalized.length())
57 : OUString();
58 // replace any minus signs with hyphen-minus so that negative numbers
59 // from the simple number formatter and heavy-duty pattern formatter
60 // agree as to their negative number sign
61 return sRet.replace(0x2212, '-');
66 * For this method to properly return the ordinal suffix for other locales
67 * than english ones, ICU 4.2+ has to be used.
69 uno::Sequence< OUString > SAL_CALL OrdinalSuffixService::getOrdinalSuffix( sal_Int32 nNumber,
70 const lang::Locale &rLocale ) throw( RuntimeException, std::exception )
72 uno::Sequence< OUString > retValue;
74 // Get the value from ICU
75 UErrorCode nCode = U_ZERO_ERROR;
76 const icu::Locale aIcuLocale( LanguageTagIcu::getIcuLocale( LanguageTag( rLocale)));
78 icu::RuleBasedNumberFormat formatter(icu::URBNF_ORDINAL, aIcuLocale, nCode);
79 if (!U_SUCCESS(nCode))
80 return retValue;
82 boost::scoped_ptr<NumberFormat> xNumberFormat(icu::NumberFormat::createInstance(aIcuLocale, nCode));
83 if (!U_SUCCESS(nCode))
84 return retValue;
86 icu::UnicodeString sFormatWithNoOrdinal;
87 icu::Formattable ftmNumber((int32_t)nNumber);
88 icu::FieldPosition icuPosA;
89 xNumberFormat->format(ftmNumber, sFormatWithNoOrdinal, icuPosA, nCode);
90 if (!U_SUCCESS(nCode))
91 return retValue;
93 OUString sValueWithNoOrdinal = mungeUnicodeStringToOUString(sFormatWithNoOrdinal, nCode);
94 if (!U_SUCCESS(nCode))
95 return retValue;
97 int32_t nRuleSets = formatter.getNumberOfRuleSetNames( );
98 for (int32_t i = 0; i < nRuleSets; ++i)
100 icu::UnicodeString ruleSet = formatter.getRuleSetName(i);
102 // format the string
103 icu::UnicodeString sFormatWithOrdinal;
104 icu::FieldPosition icuPosB;
105 formatter.format((int32_t)nNumber, ruleSet, sFormatWithOrdinal, icuPosB, nCode);
107 if (!U_SUCCESS(nCode))
108 continue;
110 OUString sValueWithOrdinal = mungeUnicodeStringToOUString(sFormatWithOrdinal, nCode);
111 if (!U_SUCCESS(nCode))
112 continue;
114 // fdo#54486 lets make sure that the ordinal format and the non-ordinal
115 // format match at the start, so that the expectation can be verified
116 // that there is some trailing "ordinal suffix" which can be extracted
117 bool bSimpleOrdinalSuffix = sValueWithOrdinal.startsWith(sValueWithNoOrdinal);
119 SAL_WARN_IF(!bSimpleOrdinalSuffix, "i18npool", "ordinal " <<
120 sValueWithOrdinal << " didn't start with expected " <<
121 sValueWithNoOrdinal << " prefix");
123 if (!bSimpleOrdinalSuffix)
124 continue;
126 // Remove the number to get the prefix
127 sal_Int32 len = sValueWithNoOrdinal.getLength();
129 sal_Int32 newLength = retValue.getLength() + 1;
130 retValue.realloc( newLength );
131 retValue[ newLength - 1 ] = sValueWithOrdinal.copy( len );
134 return retValue;
137 const sal_Char cOrdinalSuffix[] = "com.sun.star.i18n.OrdinalSuffix";
139 OUString SAL_CALL OrdinalSuffixService::getImplementationName() throw( RuntimeException, std::exception )
141 return OUString(cOrdinalSuffix);
144 sal_Bool SAL_CALL OrdinalSuffixService::supportsService( const OUString& rServiceName) throw( RuntimeException, std::exception )
146 return cppu::supportsService(this, rServiceName);
149 Sequence< OUString > SAL_CALL OrdinalSuffixService::getSupportedServiceNames() throw( RuntimeException, std::exception )
151 Sequence< OUString > aRet(1);
152 aRet[0] = cOrdinalSuffix;
153 return aRet;
156 } } } }
158 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
159 com_sun_star_i18n_OrdinalSuffix_get_implementation(
160 css::uno::XComponentContext *,
161 css::uno::Sequence<css::uno::Any> const &)
163 return cppu::acquire(new css::i18n::OrdinalSuffixService());
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */