i18nlangtag/qa/cppunit/test_languagetag.cxx | 16 ++++++++++ i18nlangtag/source/languagetag/languagetag.cxx | 40 +++++++++++++++++++++++++ include/i18nlangtag/languagetag.hxx | 16 ++++++++++ 3 files changed, 72 insertions(+)
New commits: commit b8e8c9c471ba6974886dd624630144ef2edccb2c Author: Eike Rathke <er...@redhat.com> Date: Wed May 21 14:14:56 2014 +0200 add unit test for static isValidBcp47() method Change-Id: Ib234fb1d14087236d337dfe351aea941945e718c diff --git a/i18nlangtag/qa/cppunit/test_languagetag.cxx b/i18nlangtag/qa/cppunit/test_languagetag.cxx index 93506fb..b86e263 100644 --- a/i18nlangtag/qa/cppunit/test_languagetag.cxx +++ b/i18nlangtag/qa/cppunit/test_languagetag.cxx @@ -586,6 +586,22 @@ void TestLanguageTag::testAllTags() CPPUNIT_ASSERT( uab.isIsoLocale() == false ); CPPUNIT_ASSERT( uab.isIsoODF() == false ); } + + // test static isValidBcp47() method + { + OUString aCanonicalized; + CPPUNIT_ASSERT( LanguageTag::isValidBcp47( "en-US", &aCanonicalized) && aCanonicalized == "en-US" ); + CPPUNIT_ASSERT( LanguageTag::isValidBcp47( "x-foobar", &aCanonicalized) && aCanonicalized == "x-foobar" ); + CPPUNIT_ASSERT( !LanguageTag::isValidBcp47( "unreg-and-bad", &aCanonicalized) ); +#if USE_LIBLANGTAG + CPPUNIT_ASSERT( LanguageTag::isValidBcp47( "de-Latn-DE", &aCanonicalized) && aCanonicalized == "de-DE" ); + /* TODO: at least some (those we know) grandfathered tags should be + * recognized by the replacement code. */ + CPPUNIT_ASSERT( LanguageTag::isValidBcp47( "en-GB-oed", &aCanonicalized) && aCanonicalized == "en-GB-oed" ); +#else + CPPUNIT_ASSERT( LanguageTag::isValidBcp47( "de-Latn-DE", &aCanonicalized) && aCanonicalized == "de-Latn-DE" ); +#endif + } } static bool checkMapping( const OUString& rStr1, const OUString& rStr2 ) commit a6e6cc49bfbf594aa59804ee0d9751d5ff19caba Author: Eike Rathke <er...@redhat.com> Date: Wed May 21 14:13:23 2014 +0200 add static LanguageTag::isValidBcp47() Change-Id: I2c646b3e2f13a6fccc845ce8eb82fccee154f3c6 diff --git a/i18nlangtag/source/languagetag/languagetag.cxx b/i18nlangtag/source/languagetag/languagetag.cxx index 33816b3..8a96bde 100644 --- a/i18nlangtag/source/languagetag/languagetag.cxx +++ b/i18nlangtag/source/languagetag/languagetag.cxx @@ -2685,4 +2685,44 @@ com::sun::star::lang::Locale LanguageTag::convertToLocaleWithFallback( const OUS return LanguageTag( rBcp47).makeFallback().getLocale( true); } + +// static +bool LanguageTag::isValidBcp47( const OUString& rString, OUString* o_pCanonicalized ) +{ + struct guard + { + lt_tag_t* mpLangtag; + guard() + { + theDataRef::get().incRef(); + mpLangtag = lt_tag_new(); + } + ~guard() + { + lt_tag_unref( mpLangtag); + theDataRef::get().decRef(); + } + } aVar; + + myLtError aError; + + if (lt_tag_parse( aVar.mpLangtag, OUStringToOString( rString, RTL_TEXTENCODING_UTF8).getStr(), &aError.p)) + { + char* pTag = lt_tag_canonicalize( aVar.mpLangtag, &aError.p); + SAL_WARN_IF( !pTag, "i18nlangtag", "LanguageTag:isValidBcp47: could not canonicalize '" << rString << "'"); + if (pTag) + { + if (o_pCanonicalized) + *o_pCanonicalized = OUString::createFromAscii( pTag); + free( pTag); + return true; + } + } + else + { + SAL_INFO( "i18nlangtag", "LanguageTag:isValidBcp47: could not parse '" << rString << "'"); + } + return false; +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/i18nlangtag/languagetag.hxx b/include/i18nlangtag/languagetag.hxx index dcb95c6..0e12235 100644 --- a/include/i18nlangtag/languagetag.hxx +++ b/include/i18nlangtag/languagetag.hxx @@ -228,6 +228,8 @@ public: /** If this is a valid BCP 47 language tag. Always resolves an empty tag to the system locale. + + @seealso static bool isValidBcp47(const OUString&) */ bool isValidBcp47() const; @@ -479,6 +481,20 @@ public: */ static com::sun::star::lang::Locale convertToLocaleWithFallback( const OUString& rBcp47 ); + /** If rString represents a valid BCP 47 language tag. + + Never resolves an empty tag to the system locale, in fact an empty + string is invalid here. Does not create an instance to be registered + with a conversion to Locale or LanguageType. + + @param o_pCanonicalized + If given and rString is a valid BCP 47 language tag, the + canonicalized form is assigned, which may differ from the + original string even if that was a valid tag. If rString is not + a valid tag, nothing is assigned. + */ + static bool isValidBcp47( const OUString& rString, OUString* o_pCanonicalized = NULL ); + /** If nLang is a generated on-the-fly LangID */ static bool isOnTheFlyID( LanguageType nLang ); _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits