Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / i18npool / source / ordinalsuffix / ordinalsuffix.cxx
blob95e13415c709da745b33e2bd399ad422caef67df
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <comphelper/processfactory.hxx>
30 #include <string.h>
31 #include "ordinalsuffix.hxx"
33 #include <unicode/rbnf.h>
34 #include <unicode/normlzr.h>
36 #define CSTR( ouStr ) rtl::OUStringToOString( ouStr, RTL_TEXTENCODING_UTF8 ).getStr( )
38 using namespace ::com::sun::star::i18n;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star;
41 using namespace ::rtl;
43 namespace com { namespace sun { namespace star { namespace i18n {
46 OrdinalSuffix::OrdinalSuffix(
47 const com::sun::star::uno::Reference < com::sun::star::lang::XMultiServiceFactory >& rxMSF) :
48 _xServiceManager( rxMSF )
52 OrdinalSuffix::~OrdinalSuffix()
58 * For this method to properly return the ordinal suffix for other locales
59 * than english ones, ICU 4.2+ has to be used.
61 uno::Sequence< OUString > SAL_CALL OrdinalSuffix::getOrdinalSuffix( sal_Int32 nNumber,
62 const lang::Locale &aLocale ) throw( RuntimeException )
64 uno::Sequence< OUString > retValue;
66 // Get the value from ICU
67 UErrorCode nCode = U_ZERO_ERROR;
68 const icu::Locale rIcuLocale(
69 CSTR( aLocale.Language ),
70 CSTR( aLocale.Country ),
71 CSTR( aLocale.Variant ) );
72 icu::RuleBasedNumberFormat formatter(
73 icu::URBNF_ORDINAL, rIcuLocale, nCode );
75 if ( U_SUCCESS( nCode ) )
77 int32_t nRuleSets = formatter.getNumberOfRuleSetNames( );
78 for ( int32_t i = 0; i < nRuleSets; i++ )
80 icu::UnicodeString ruleSet = formatter.getRuleSetName( i );
81 // format the string
82 icu::UnicodeString icuRet;
83 icu::FieldPosition icuPos;
84 formatter.format( (int32_t)nNumber, ruleSet, icuRet, icuPos, nCode );
86 if ( U_SUCCESS( nCode ) )
88 // Apply NFKC normalization to get normal letters
89 icu::UnicodeString normalized;
90 nCode = U_ZERO_ERROR;
91 icu::Normalizer::normalize( icuRet, UNORM_NFKC, 0, normalized, nCode );
92 if ( U_SUCCESS( nCode ) && ( normalized != icuRet ) )
94 // Convert the normalized UnicodeString to OUString
95 OUString sValue( reinterpret_cast<const sal_Unicode *>( normalized.getBuffer( ) ), normalized.length() );
97 // Remove the number to get the prefix
98 sal_Int32 len = OUString::valueOf( nNumber ).getLength( );
100 sal_Int32 newLength = retValue.getLength() + 1;
101 retValue.realloc( newLength );
102 retValue[ newLength - 1 ] = sValue.copy( len );
108 return retValue;
112 const sal_Char cOrdinalSuffix[] = "com.sun.star.i18n.OrdinalSuffix";
114 OUString SAL_CALL OrdinalSuffix::getImplementationName(void) throw( RuntimeException )
116 return OUString::createFromAscii(cOrdinalSuffix);
119 sal_Bool SAL_CALL OrdinalSuffix::supportsService( const OUString& rServiceName) throw( RuntimeException )
121 return !rServiceName.compareToAscii(cOrdinalSuffix);
124 Sequence< OUString > SAL_CALL OrdinalSuffix::getSupportedServiceNames(void) throw( RuntimeException )
126 Sequence< OUString > aRet(1);
127 aRet[0] = OUString::createFromAscii(cOrdinalSuffix);
128 return aRet;
131 } } } }
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */