diff options
author | Eike Rathke <erack@redhat.com> | 2014-05-21 14:13:23 +0200 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2014-05-21 14:15:24 +0200 |
commit | a6e6cc49bfbf594aa59804ee0d9751d5ff19caba (patch) | |
tree | a0b22ab7085a7ba7016cd8636e75b1231f99495d | |
parent | f824604b5c1b480bf6680935362eb1d4617187c9 (diff) |
add static LanguageTag::isValidBcp47()
Change-Id: I2c646b3e2f13a6fccc845ce8eb82fccee154f3c6
-rw-r--r-- | i18nlangtag/source/languagetag/languagetag.cxx | 40 | ||||
-rw-r--r-- | include/i18nlangtag/languagetag.hxx | 16 |
2 files changed, 56 insertions, 0 deletions
diff --git a/i18nlangtag/source/languagetag/languagetag.cxx b/i18nlangtag/source/languagetag/languagetag.cxx index 33816b3c7e0f..8a96bde83f81 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 dcb95c61d911..0e12235c34ba 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 ); |