diff options
author | Eike Rathke <erack@redhat.com> | 2013-04-03 01:16:18 +0200 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2013-04-03 01:42:10 +0200 |
commit | da5adeada29b51dd0a65582cff9f0913e556f770 (patch) | |
tree | 2f25399d1ad58cdc3dd995d5423786b4bebfaff9 | |
parent | e72072029ad22fdde8310e22667fd8f2f46db28e (diff) |
DescriptionInfoset with LanguageTag
Change-Id: I7957a4a773b2ea8e69c30a1152b90ce5623a26dc
-rw-r--r-- | desktop/Library_deployment.mk | 1 | ||||
-rw-r--r-- | desktop/source/deployment/inc/dp_descriptioninfoset.hxx | 15 | ||||
-rw-r--r-- | desktop/source/deployment/inc/dp_resource.h | 12 | ||||
-rw-r--r-- | desktop/source/deployment/misc/dp_descriptioninfoset.cxx | 86 | ||||
-rw-r--r-- | desktop/source/deployment/misc/dp_resource.cxx | 21 | ||||
-rw-r--r-- | desktop/source/deployment/registry/package/dp_package.cxx | 41 |
6 files changed, 57 insertions, 119 deletions
diff --git a/desktop/Library_deployment.mk b/desktop/Library_deployment.mk index b378562a2400..dc30097c9511 100644 --- a/desktop/Library_deployment.mk +++ b/desktop/Library_deployment.mk @@ -49,6 +49,7 @@ $(eval $(call gb_Library_use_libraries,deployment,\ ucbhelper \ utl \ xmlscript \ + i18nisolang1 \ $(gb_UWINAPI) \ )) diff --git a/desktop/source/deployment/inc/dp_descriptioninfoset.hxx b/desktop/source/deployment/inc/dp_descriptioninfoset.hxx index 8a5361be372e..1e4817fff17d 100644 --- a/desktop/source/deployment/inc/dp_descriptioninfoset.hxx +++ b/desktop/source/deployment/inc/dp_descriptioninfoset.hxx @@ -31,7 +31,6 @@ /// @HTML namespace com { namespace sun { namespace star { - namespace lang { struct Locale; } namespace uno { class XComponentContext; } namespace xml { namespace dom { @@ -230,16 +229,9 @@ private: SAL_DLLPRIVATE ::com::sun::star::uno::Reference< ::com::sun::star::xml::dom::XNode > getLocalizedChild( ::rtl::OUString const & sParent) const; SAL_DLLPRIVATE ::com::sun::star::uno::Reference< ::com::sun::star::xml::dom::XNode> - matchFullLocale(::com::sun::star::uno::Reference< - ::com::sun::star::xml::dom::XNode > const & xParent, ::rtl::OUString const & sLocale) const; - SAL_DLLPRIVATE ::com::sun::star::uno::Reference< ::com::sun::star::xml::dom::XNode> - matchCountryAndLanguage(::com::sun::star::uno::Reference< - ::com::sun::star::xml::dom::XNode > const & xParent, - ::com::sun::star::lang::Locale const & officeLocale) const; - SAL_DLLPRIVATE ::com::sun::star::uno::Reference< ::com::sun::star::xml::dom::XNode> - matchLanguage( + matchLanguageTag( ::com::sun::star::uno::Reference< ::com::sun::star::xml::dom::XNode > const & xParent, - ::com::sun::star::lang::Locale const & officeLocale) const; + OUString const & rTag) const; /** If there is no child element with a locale matching the office locale, then we use the first child. In the case of the simple-license we also use the former default locale, which @@ -258,9 +250,6 @@ private: SAL_DLLPRIVATE ::rtl::OUString getLocalizedHREFAttrFromChild( ::rtl::OUString const & sXPathParent, bool * out_bParentExists) const; - static SAL_DLLPRIVATE ::rtl::OUString - localeToString(::com::sun::star::lang::Locale const & locale); - /** Gets the node value for a given expression. The expression is used in m_xpath-selectSingleNode. The value of the returned node is return value of this function. diff --git a/desktop/source/deployment/inc/dp_resource.h b/desktop/source/deployment/inc/dp_resource.h index 2f94bbd2a2dd..9f918780db41 100644 --- a/desktop/source/deployment/inc/dp_resource.h +++ b/desktop/source/deployment/inc/dp_resource.h @@ -22,7 +22,7 @@ #include "tools/string.hxx" #include "tools/resid.hxx" -#include "com/sun/star/lang/Locale.hpp" +#include <i18npool/languagetag.hxx> #include "dp_misc.h" #include <memory> #include "dp_misc_api.hxx" @@ -43,15 +43,7 @@ struct StaticResourceString : //============================================================================== DESKTOP_DEPLOYMENTMISC_DLLPUBLIC -::com::sun::star::lang::Locale toLocale( ::rtl::OUString const & slang ); - -//============================================================================== -DESKTOP_DEPLOYMENTMISC_DLLPUBLIC -::com::sun::star::lang::Locale getOfficeLocale(); - -//============================================================================== -DESKTOP_DEPLOYMENTMISC_DLLPUBLIC -::rtl::OUString getOfficeLocaleString(); +const LanguageTag & getOfficeLanguageTag(); } diff --git a/desktop/source/deployment/misc/dp_descriptioninfoset.cxx b/desktop/source/deployment/misc/dp_descriptioninfoset.cxx index dc7193fe4bcc..8daef83d3918 100644 --- a/desktop/source/deployment/misc/dp_descriptioninfoset.cxx +++ b/desktop/source/deployment/misc/dp_descriptioninfoset.cxx @@ -33,7 +33,6 @@ #include "com/sun/star/beans/XPropertySet.hpp" #include "com/sun/star/io/SequenceInputStream.hpp" #include "com/sun/star/lang/XMultiComponentFactory.hpp" -#include "com/sun/star/lang/Locale.hpp" #include "com/sun/star/uno/Reference.hxx" #include "com/sun/star/uno/RuntimeException.hpp" #include "com/sun/star/uno/Sequence.hxx" @@ -725,20 +724,24 @@ DescriptionInfoset::getLocalizedChild( const ::rtl::OUString & sParent) const css::uno::Reference<css::xml::dom::XNode> nodeMatch; if (xParent.is()) { - const ::rtl::OUString sLocale = getOfficeLocaleString(); - nodeMatch = matchFullLocale(xParent, sLocale); + nodeMatch = matchLanguageTag(xParent, getOfficeLanguageTag().getBcp47()); //office: en-DE, en, en-DE-altmark if (! nodeMatch.is()) { - const css::lang::Locale officeLocale = getOfficeLocale(); - nodeMatch = matchCountryAndLanguage(xParent, officeLocale); - if ( ! nodeMatch.is()) + const ::std::vector< OUString > aFallbacks = getOfficeLanguageTag().getFallbackStrings(); + // Already tried full tag, continue with first fallback. + ::std::vector< OUString >::const_iterator it( aFallbacks.begin()); + if (it != aFallbacks.end()) + ++it; + for ( ; it != aFallbacks.end(); ++it) { - nodeMatch = matchLanguage(xParent, officeLocale); - if (! nodeMatch.is()) - nodeMatch = getChildWithDefaultLocale(xParent); + nodeMatch = matchLanguageTag(xParent, *it); + if (nodeMatch.is()) + break; } + if (! nodeMatch.is()) + nodeMatch = getChildWithDefaultLocale(xParent); } } @@ -746,79 +749,26 @@ DescriptionInfoset::getLocalizedChild( const ::rtl::OUString & sParent) const } css::uno::Reference<css::xml::dom::XNode> -DescriptionInfoset::matchFullLocale(css::uno::Reference< css::xml::dom::XNode > - const & xParent, ::rtl::OUString const & sLocale) const -{ - OSL_ASSERT(xParent.is()); - const ::rtl::OUString exp1("*[@lang=\"" + sLocale + "\"]"); - try { - return m_xpath->selectSingleNode(xParent, exp1); - } catch (const css::xml::xpath::XPathException &) { - // ignore - return 0; - } -} - -css::uno::Reference<css::xml::dom::XNode> -DescriptionInfoset::matchCountryAndLanguage( - css::uno::Reference< css::xml::dom::XNode > const & xParent, css::lang::Locale const & officeLocale) const -{ - OSL_ASSERT(xParent.is()); - css::uno::Reference<css::xml::dom::XNode> nodeMatch; - - if (!officeLocale.Country.isEmpty()) - { - const ::rtl::OUString sLangCountry(officeLocale.Language + - "-" + officeLocale.Country); - //first try exact match for lang-country - const ::rtl::OUString exp1( - "*[@lang=\"" + sLangCountry +"\"]"); - try { - nodeMatch = m_xpath->selectSingleNode(xParent, exp1); - } catch (const css::xml::xpath::XPathException &) { - // ignore - } - - //try to match in strings that also have a variant, for example en-US matches in - //en-US-montana - if (!nodeMatch.is()) - { - const ::rtl::OUString exp2( - "*[starts-with(@lang,\"" + sLangCountry + "-\")]"); - try { - nodeMatch = m_xpath->selectSingleNode(xParent, exp2); - } catch (const css::xml::xpath::XPathException &) { - // ignore - } - } - } - - return nodeMatch; -} - - -css::uno::Reference<css::xml::dom::XNode> -DescriptionInfoset::matchLanguage( - css::uno::Reference< css::xml::dom::XNode > const & xParent, css::lang::Locale const & officeLocale) const +DescriptionInfoset::matchLanguageTag( + css::uno::Reference< css::xml::dom::XNode > const & xParent, OUString const & rTag) const { OSL_ASSERT(xParent.is()); css::uno::Reference<css::xml::dom::XNode> nodeMatch; //first try exact match for lang - const ::rtl::OUString exp1("*[@lang=\"" + officeLocale.Language - + "\"]"); + const ::rtl::OUString exp1("*[@lang=\"" + rTag + "\"]"); try { nodeMatch = m_xpath->selectSingleNode(xParent, exp1); } catch (const css::xml::xpath::XPathException &) { // ignore } - //try to match in strings that also have a country and/orvariant, for example en matches in - //en-US-montana, en-US, en-montana + //try to match in strings that also have a country and/or variant, for + //example en matches in en-US-montana, en-US, en-montana if (!nodeMatch.is()) { const ::rtl::OUString exp2( - "*[starts-with(@lang,\"" + officeLocale.Language + "-\")]"); + "*[starts-with(@lang,\"" + rTag + "-\")]"); try { nodeMatch = m_xpath->selectSingleNode(xParent, exp2); } catch (const css::xml::xpath::XPathException &) { diff --git a/desktop/source/deployment/misc/dp_resource.cxx b/desktop/source/deployment/misc/dp_resource.cxx index 2073578e1988..c7acb758b95d 100644 --- a/desktop/source/deployment/misc/dp_resource.cxx +++ b/desktop/source/deployment/misc/dp_resource.cxx @@ -37,21 +37,21 @@ namespace dp_misc { namespace { struct OfficeLocale : - public rtl::StaticWithInit<OUString, OfficeLocale> { - const OUString operator () () { + public rtl::StaticWithInit<LanguageTag, OfficeLocale> { + const LanguageTag operator () () { OUString slang(utl::ConfigManager::getLocale()); //fallback, the locale is currently only set when the user starts the //office for the first time. if (slang.isEmpty()) slang = "en-US"; - return slang; + return LanguageTag( slang); } }; struct DeploymentResMgr : public rtl::StaticWithInit< ResMgr *, DeploymentResMgr> { ResMgr * operator () () { - return ResMgr::CreateResMgr( "deployment", LanguageTag( OfficeLocale::get()) ); + return ResMgr::CreateResMgr( "deployment", OfficeLocale::get() ); } }; @@ -76,19 +76,8 @@ String getResourceString( sal_uInt16 id ) return ret; } -//============================================================================= -::com::sun::star::lang::Locale toLocale( ::rtl::OUString const & slang ) -{ - return LanguageTag( slang).getLocale(); -} - //============================================================================== -lang::Locale getOfficeLocale() -{ - return toLocale(OfficeLocale::get()); -} - -::rtl::OUString getOfficeLocaleString() +const LanguageTag & getOfficeLanguageTag() { return OfficeLocale::get(); } diff --git a/desktop/source/deployment/registry/package/dp_package.cxx b/desktop/source/deployment/registry/package/dp_package.cxx index ef94106f3591..201147b79965 100644 --- a/desktop/source/deployment/registry/package/dp_package.cxx +++ b/desktop/source/deployment/registry/package/dp_package.cxx @@ -1419,9 +1419,11 @@ void BackendImpl::PackageImpl::scanBundle( } - const lang::Locale officeLocale = getOfficeLocale(); + const LanguageTag& officeLocale = getOfficeLanguageTag(); + const ::std::vector< OUString > officeFallbacks( officeLocale.getFallbackStrings()); + const size_t nPenaltyMax = ::std::numeric_limits<size_t>::max(); + size_t descrPenalty = nPenaltyMax; OUString descrFile; - lang::Locale descrFileLocale; const Reference<XComponentContext> xContext( getMyBackend()->getComponentContext() ); @@ -1469,18 +1471,33 @@ void BackendImpl::PackageImpl::scanBundle( } else { // match best locale: - lang::Locale locale( toLocale(param->m_sValue) ); - if (locale.Language == officeLocale.Language) + LanguageTag descrTag( param->m_sValue); + if (officeLocale.getLanguage() == descrTag.getLanguage()) { - if (descrFileLocale.Country == officeLocale.Country - && locale.Country != officeLocale.Country) - continue; - if (descrFileLocale.Variant == officeLocale.Variant - && locale.Variant != officeLocale.Variant) - continue; - descrFile = url; - descrFileLocale = locale; + size_t nPenalty = nPenaltyMax; + const ::std::vector< OUString > descrFallbacks( descrTag.getFallbackStrings()); + for (size_t o=0; o < officeFallbacks.size() && nPenalty == nPenaltyMax; ++o) + { + for (size_t d=0; d < descrFallbacks.size() && nPenalty == nPenaltyMax; ++d) + { + if (officeFallbacks[o] == descrFallbacks[d]) + { + // The last fallbacks are always language-only + // fallbacks, so we _will_ have _some_ match if + // we ever entered the overall if() condition. + nPenalty = o * 1000 + d; + if (descrPenalty > nPenalty) + { + descrPenalty = nPenalty; + descrFile = url; + } + } + } + } } + // TODO: we could break here if descrPenalty==0 for an exact + // match of officeLocale, but the previous code didn't; are + // there side effects? } continue; } |