1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
22 #include <string_view>
24 #include <localizationmgr.hxx>
26 #include <basidesh.hxx>
27 #include <baside3.hxx>
29 #include <iderdll.hxx>
31 #include <managelang.hxx>
33 #include <com/sun/star/frame/XLayoutManager.hpp>
34 #include <com/sun/star/resource/MissingResourceException.hpp>
35 #include <com/sun/star/resource/XStringResourceSupplier.hpp>
36 #include <sfx2/bindings.hxx>
37 #include <sfx2/sfxsids.hrc>
38 #include <sfx2/viewfrm.hxx>
39 #include <tools/debug.hxx>
41 #include <osl/diagnose.h>
42 #include <o3tl/string_view.hxx>
47 using namespace ::com::sun::star
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::lang
;
50 using namespace ::com::sun::star::beans
;
51 using namespace ::com::sun::star::resource
;
56 constexpr OUString
aDot(u
"."_ustr
);
57 constexpr OUString
aEsc(u
"&"_ustr
);
58 constexpr OUString
aSemi(u
";"_ustr
);
62 LocalizationMgr::LocalizationMgr(
64 ScriptDocument aDocument
,
66 Reference
<XStringResourceManager
> const& xStringResourceManager
68 m_xStringResourceManager(xStringResourceManager
),
70 m_aDocument(std::move(aDocument
)),
71 m_aLibName(std::move(aLibName
))
74 bool LocalizationMgr::isLibraryLocalized ()
76 if (m_xStringResourceManager
.is())
77 return m_xStringResourceManager
->getLocales().hasElements();
81 void LocalizationMgr::handleTranslationbar ()
83 static constexpr OUString aToolBarResName
= u
"private:resource/toolbar/translationbar"_ustr
;
85 Reference
< beans::XPropertySet
> xFrameProps
86 ( m_pShell
->GetViewFrame().GetFrame().GetFrameInterface(), uno::UNO_QUERY
);
87 if ( !xFrameProps
.is() )
90 Reference
< css::frame::XLayoutManager
> xLayoutManager
;
91 uno::Any a
= xFrameProps
->getPropertyValue( "LayoutManager" );
93 if ( xLayoutManager
.is() )
95 if ( !isLibraryLocalized() )
97 xLayoutManager
->destroyElement( aToolBarResName
);
101 xLayoutManager
->createElement( aToolBarResName
);
102 xLayoutManager
->requestElement( aToolBarResName
);
108 // TODO: -> export from toolkit
111 static bool isLanguageDependentProperty( std::u16string_view aName
)
116 sal_Int32 nNameLength
;
124 { "CurrencySymbol", 14 },
125 { "StringItemList", 14 },
129 for (Prop
const* pProp
= vProp
; pProp
->sName
; ++pProp
)
130 if (o3tl::equalsAscii(aName
, std::string_view(pProp
->sName
, pProp
->nNameLength
)))
136 void LocalizationMgr::implEnableDisableResourceForAllLibraryDialogs( HandleResourceMode eMode
)
138 Sequence
< OUString
> aDlgNames
= m_aDocument
.getObjectNames( E_DIALOGS
, m_aLibName
);
139 sal_Int32 nDlgCount
= aDlgNames
.getLength();
140 const OUString
* pDlgNames
= aDlgNames
.getConstArray();
142 Reference
< XStringResourceResolver
> xDummyStringResolver
;
143 for( sal_Int32 i
= 0 ; i
< nDlgCount
; i
++ )
145 OUString aDlgName
= pDlgNames
[ i
];
146 if (VclPtr
<DialogWindow
> pWin
= m_pShell
->FindDlgWin(m_aDocument
, m_aLibName
, aDlgName
))
148 Reference
< container::XNameContainer
> xDialog
= pWin
->GetDialog();
151 // Handle dialog itself as control
153 aDialogCtrl
<<= xDialog
;
154 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
155 std::u16string_view(), m_xStringResourceManager
, xDummyStringResolver
, eMode
);
157 // Handle all controls
158 Sequence
< OUString
> aNames
= xDialog
->getElementNames();
159 const OUString
* pNames
= aNames
.getConstArray();
160 sal_Int32 nCtrls
= aNames
.getLength();
161 for( sal_Int32 j
= 0 ; j
< nCtrls
; ++j
)
163 OUString
aCtrlName( pNames
[j
] );
164 Any aCtrl
= xDialog
->getByName( aCtrlName
);
165 implHandleControlResourceProperties( aCtrl
, aDlgName
,
166 aCtrlName
, m_xStringResourceManager
, xDummyStringResolver
, eMode
);
174 static OUString implCreatePureResourceId
175 ( std::u16string_view aDialogName
, std::u16string_view aCtrlName
,
176 std::u16string_view aPropName
,
177 const Reference
< XStringResourceManager
>& xStringResourceManager
)
179 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
180 OUString aPureIdStr
= OUString::number( nUniqueId
)
184 if( !aCtrlName
.empty() )
186 aPureIdStr
+= aCtrlName
+ aDot
;
188 aPureIdStr
+= aPropName
;
192 // Works on xStringResourceManager's current language for SET_IDS/RESET_IDS,
193 // anyway only one language should exist when calling this method then,
194 // either the first one for mode SET_IDS or the last one for mode RESET_IDS
195 sal_Int32
LocalizationMgr::implHandleControlResourceProperties
196 (const Any
& rControlAny
, std::u16string_view aDialogName
, std::u16string_view aCtrlName
,
197 const Reference
< XStringResourceManager
>& xStringResourceManager
,
198 const Reference
< XStringResourceResolver
>& xSourceStringResolver
, HandleResourceMode eMode
)
200 sal_Int32 nChangedCount
= 0;
202 Reference
< XPropertySet
> xPropertySet
;
203 rControlAny
>>= xPropertySet
;
204 if( xPropertySet
.is() && xStringResourceManager
.is())
206 Sequence
< Locale
> aLocaleSeq
= xStringResourceManager
->getLocales();
207 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
208 if( nLocaleCount
== 0 )
211 Reference
< XPropertySetInfo
> xPropertySetInfo
= xPropertySet
->getPropertySetInfo();
212 if( xPropertySetInfo
.is() )
214 // get sequence of control properties
215 Sequence
< Property
> aPropSeq
= xPropertySetInfo
->getProperties();
216 const Property
* pProps
= aPropSeq
.getConstArray();
217 sal_Int32 nCtrlProps
= aPropSeq
.getLength();
219 // create a map of tab indices and control names, sorted by tab index
220 for( sal_Int32 j
= 0 ; j
< nCtrlProps
; ++j
)
222 const Property
& rProp
= pProps
[j
];
223 OUString aPropName
= rProp
.Name
;
224 TypeClass eType
= rProp
.Type
.getTypeClass();
225 bool bLanguageDependentProperty
=
226 (eType
== TypeClass_STRING
|| eType
== TypeClass_SEQUENCE
)
227 && isLanguageDependentProperty( aPropName
);
228 if( !bLanguageDependentProperty
)
231 if( eType
== TypeClass_STRING
)
233 Any aPropAny
= xPropertySet
->getPropertyValue( aPropName
);
235 aPropAny
>>= aPropStr
;
237 // Replace string by id, add id+string to StringResource
238 if( eMode
== SET_IDS
)
240 bool bEscAlreadyExisting
= aPropStr
.startsWith("&");
241 if( bEscAlreadyExisting
)
244 OUString aPureIdStr
= implCreatePureResourceId
245 ( aDialogName
, aCtrlName
, aPropName
, xStringResourceManager
);
247 // Set Id for all locales
248 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
249 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
251 const Locale
& rLocale
= pLocales
[ i
];
252 xStringResourceManager
->setStringForLocale( aPureIdStr
, aPropStr
, rLocale
);
255 OUString aPropIdStr
= aEsc
+ aPureIdStr
;
256 // TODO?: Change here and in toolkit
258 xPropertySet
->setPropertyValue( aPropName
, Any(aPropIdStr
) );
260 // Replace id by string from StringResource
261 else if( eMode
== RESET_IDS
)
263 if( aPropStr
.getLength() > 1 )
265 OUString aPureIdStr
= aPropStr
.copy( 1 );
266 OUString aNewPropStr
= aPropStr
;
269 aNewPropStr
= xStringResourceManager
->resolveString( aPureIdStr
);
271 catch(const MissingResourceException
&)
274 xPropertySet
->setPropertyValue( aPropName
, Any(aNewPropStr
) );
277 // Remove Id for all locales
278 else if( eMode
== REMOVE_IDS_FROM_RESOURCE
)
280 if( aPropStr
.getLength() > 1 )
282 OUString aPureIdStr
= aPropStr
.copy( 1 );
284 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
285 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
287 const Locale
& rLocale
= pLocales
[ i
];
290 xStringResourceManager
->removeIdForLocale( aPureIdStr
, rLocale
);
292 catch(const MissingResourceException
&)
298 // Rename resource id
299 else if( eMode
== RENAME_DIALOG_IDS
|| eMode
== RENAME_CONTROL_IDS
)
301 OUString aPureSourceIdStr
= aPropStr
.copy( 1 );
303 OUString aPureIdStr
= implCreatePureResourceId
304 ( aDialogName
, aCtrlName
, aPropName
, xStringResourceManager
);
306 // Set new Id and remove old one for all locales
307 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
308 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
310 const Locale
& rLocale
= pLocales
[ i
];
313 OUString aResStr
= xStringResourceManager
->resolveStringForLocale
314 ( aPureSourceIdStr
, rLocale
);
315 xStringResourceManager
->removeIdForLocale( aPureSourceIdStr
, rLocale
);
316 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
318 catch(const MissingResourceException
&)
322 OUString aPropIdStr
= aEsc
+ aPureIdStr
;
323 // TODO?: Change here and in toolkit
325 xPropertySet
->setPropertyValue( aPropName
, Any(aPropIdStr
) );
327 // Replace string by string from source StringResourceResolver
328 else if( eMode
== MOVE_RESOURCES
&& xSourceStringResolver
.is() )
330 OUString aPureSourceIdStr
= aPropStr
.copy( 1 );
332 OUString aPureIdStr
= implCreatePureResourceId
333 ( aDialogName
, aCtrlName
, aPropName
, xStringResourceManager
);
335 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
337 // Set Id for all locales
338 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
339 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
341 const Locale
& rLocale
= pLocales
[ i
];
345 aResStr
= xSourceStringResolver
->resolveStringForLocale
346 ( aPureSourceIdStr
, rLocale
);
348 catch(const MissingResourceException
&)
350 aResStr
= xSourceStringResolver
->resolveStringForLocale
351 ( aPureSourceIdStr
, rDefaultLocale
);
353 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
356 OUString aPropIdStr
= aEsc
+ aPureIdStr
;
357 // TODO?: Change here and in toolkit
359 xPropertySet
->setPropertyValue( aPropName
, Any(aPropIdStr
) );
361 // Copy string from source to target resource
362 else if( eMode
== COPY_RESOURCES
&& xSourceStringResolver
.is() )
364 OUString aPureSourceIdStr
= aPropStr
.copy( 1 );
366 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
368 // Copy Id for all locales
369 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
370 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
372 const Locale
& rLocale
= pLocales
[ i
];
376 aResStr
= xSourceStringResolver
->resolveStringForLocale
377 ( aPureSourceIdStr
, rLocale
);
379 catch(const MissingResourceException
&)
381 aResStr
= xSourceStringResolver
->resolveStringForLocale
382 ( aPureSourceIdStr
, rDefaultLocale
);
384 xStringResourceManager
->setStringForLocale( aPureSourceIdStr
, aResStr
, rLocale
);
390 // Listbox / Combobox
391 else if( eType
== TypeClass_SEQUENCE
)
393 Any aPropAny
= xPropertySet
->getPropertyValue( aPropName
);
394 Sequence
< OUString
> aPropStrings
;
395 aPropAny
>>= aPropStrings
;
397 const OUString
* pPropStrings
= aPropStrings
.getConstArray();
398 sal_Int32 nPropStringCount
= aPropStrings
.getLength();
399 if( nPropStringCount
== 0 )
402 // Replace string by id, add id+string to StringResource
403 if( eMode
== SET_IDS
)
405 Sequence
< OUString
> aIdStrings
;
406 aIdStrings
.realloc( nPropStringCount
);
407 OUString
* pIdStrings
= aIdStrings
.getArray();
409 OUString aIdStrBase
= aDot
414 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
416 for ( i
= 0; i
< nPropStringCount
; ++i
)
418 OUString aPropStr
= pPropStrings
[i
];
419 bool bEscAlreadyExisting
= aPropStr
.startsWith("&");
420 if( bEscAlreadyExisting
)
422 pIdStrings
[i
] = aPropStr
;
426 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
427 OUString aPureIdStr
= OUString::number( nUniqueId
)
430 // Set Id for all locales
431 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
433 const Locale
& rLocale
= pLocales
[ iLocale
];
434 xStringResourceManager
->setStringForLocale( aPureIdStr
, aPropStr
, rLocale
);
437 pIdStrings
[i
] = aEsc
+ aPureIdStr
;
439 xPropertySet
->setPropertyValue( aPropName
, Any(aIdStrings
) );
441 // Replace id by string from StringResource
442 else if( eMode
== RESET_IDS
)
444 Sequence
< OUString
> aNewPropStrings
;
445 aNewPropStrings
.realloc( nPropStringCount
);
446 OUString
* pNewPropStrings
= aNewPropStrings
.getArray();
449 for ( i
= 0; i
< nPropStringCount
; ++i
)
451 OUString aIdStr
= pPropStrings
[i
];
452 OUString aNewPropStr
= aIdStr
;
453 if( aIdStr
.getLength() > 1 )
455 OUString aPureIdStr
= aIdStr
.copy( 1 );
458 aNewPropStr
= xStringResourceManager
->resolveString( aPureIdStr
);
460 catch(const MissingResourceException
&)
464 pNewPropStrings
[i
] = aNewPropStr
;
466 xPropertySet
->setPropertyValue( aPropName
, Any(aNewPropStrings
) );
468 // Remove Id for all locales
469 else if( eMode
== REMOVE_IDS_FROM_RESOURCE
)
471 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
473 for ( i
= 0; i
< nPropStringCount
; ++i
)
475 OUString aIdStr
= pPropStrings
[i
];
476 if( aIdStr
.getLength() > 1 )
478 OUString aPureIdStr
= aIdStr
.copy( 1 );
480 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
482 const Locale
& rLocale
= pLocales
[iLocale
];
485 xStringResourceManager
->removeIdForLocale( aPureIdStr
, rLocale
);
487 catch(const MissingResourceException
&)
494 // Rename resource id
495 else if( eMode
== RENAME_CONTROL_IDS
)
497 Sequence
< OUString
> aIdStrings
;
498 aIdStrings
.realloc( nPropStringCount
);
499 OUString
* pIdStrings
= aIdStrings
.getArray();
501 OUString aIdStrBase
= aDot
506 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
508 for ( i
= 0; i
< nPropStringCount
; ++i
)
510 OUString aSourceIdStr
= pPropStrings
[i
];
511 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
513 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
514 OUString aPureIdStr
= OUString::number( nUniqueId
)
517 // Set Id for all locales
518 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
520 const Locale
& rLocale
= pLocales
[ iLocale
];
524 OUString aResStr
= xStringResourceManager
->resolveStringForLocale
525 ( aPureSourceIdStr
, rLocale
);
526 xStringResourceManager
->removeIdForLocale( aPureSourceIdStr
, rLocale
);
527 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
529 catch(const MissingResourceException
&)
533 pIdStrings
[i
] = aEsc
+ aPureIdStr
;
535 xPropertySet
->setPropertyValue( aPropName
, Any(aIdStrings
) );
537 // Replace string by string from source StringResourceResolver
538 else if( eMode
== MOVE_RESOURCES
&& xSourceStringResolver
.is() )
540 Sequence
< OUString
> aIdStrings
;
541 aIdStrings
.realloc( nPropStringCount
);
542 OUString
* pIdStrings
= aIdStrings
.getArray();
544 OUString aIdStrBase
= aDot
549 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
551 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
553 for ( i
= 0; i
< nPropStringCount
; ++i
)
555 OUString aSourceIdStr
= pPropStrings
[i
];
556 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
558 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
559 OUString aPureIdStr
= OUString::number( nUniqueId
)
562 // Set Id for all locales
563 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
565 const Locale
& rLocale
= pLocales
[ iLocale
];
570 aResStr
= xSourceStringResolver
->resolveStringForLocale
571 ( aPureSourceIdStr
, rLocale
);
573 catch(const MissingResourceException
&)
575 aResStr
= xSourceStringResolver
->resolveStringForLocale
576 ( aPureSourceIdStr
, rDefaultLocale
);
578 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
581 pIdStrings
[i
] = aEsc
+ aPureIdStr
;
583 xPropertySet
->setPropertyValue( aPropName
, Any(aIdStrings
) );
585 // Copy string from source to target resource
586 else if( eMode
== COPY_RESOURCES
&& xSourceStringResolver
.is() )
588 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
590 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
592 for ( i
= 0; i
< nPropStringCount
; ++i
)
594 OUString aSourceIdStr
= pPropStrings
[i
];
595 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
597 // Set Id for all locales
598 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
600 const Locale
& rLocale
= pLocales
[ iLocale
];
605 aResStr
= xSourceStringResolver
->resolveStringForLocale
606 ( aPureSourceIdStr
, rLocale
);
608 catch(const MissingResourceException
&)
610 aResStr
= xSourceStringResolver
->resolveStringForLocale
611 ( aPureSourceIdStr
, rDefaultLocale
);
613 xStringResourceManager
->setStringForLocale( aPureSourceIdStr
, aResStr
, rLocale
);
622 return nChangedCount
;
626 void LocalizationMgr::handleAddLocales( const Sequence
< Locale
>& aLocaleSeq
)
628 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
629 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
631 if( isLibraryLocalized() )
633 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
635 const Locale
& rLocale
= pLocales
[ i
];
636 m_xStringResourceManager
->newLocale( rLocale
);
641 DBG_ASSERT( nLocaleCount
==1, "LocalizationMgr::handleAddLocales(): Only one first locale allowed" );
643 const Locale
& rLocale
= pLocales
[ 0 ];
644 m_xStringResourceManager
->newLocale( rLocale
);
645 enableResourceForAllLibraryDialogs();
648 MarkDocumentModified( m_aDocument
);
650 // update locale toolbar
651 if (SfxBindings
* pBindings
= GetBindingsPtr())
652 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
654 handleTranslationbar();
658 void LocalizationMgr::handleRemoveLocales( const Sequence
< Locale
>& aLocaleSeq
)
660 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
661 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
662 bool bConsistent
= true;
663 bool bModified
= false;
665 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
667 const Locale
& rLocale
= pLocales
[ i
];
670 // Check if last locale
671 Sequence
< Locale
> aResLocaleSeq
= m_xStringResourceManager
->getLocales();
672 if( aResLocaleSeq
.getLength() == 1 )
674 const Locale
& rLastResLocale
= aResLocaleSeq
.getConstArray()[ 0 ];
675 if( localesAreEqual( rLocale
, rLastResLocale
) )
677 disableResourceForAllLibraryDialogs();
681 // Inconsistency, keep last locale
691 m_xStringResourceManager
->removeLocale( rLocale
);
694 catch(const IllegalArgumentException
&)
702 MarkDocumentModified( m_aDocument
);
705 if (SfxBindings
* pBindings
= GetBindingsPtr())
707 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
708 pBindings
->Invalidate( SID_BASICIDE_MANAGE_LANG
);
711 handleTranslationbar();
714 DBG_ASSERT( bConsistent
,
715 "LocalizationMgr::handleRemoveLocales(): sequence contains unsupported locales" );
718 void LocalizationMgr::handleSetDefaultLocale(const Locale
& rLocale
)
720 if( !m_xStringResourceManager
.is() )
725 m_xStringResourceManager
->setDefaultLocale(rLocale
);
727 catch(const IllegalArgumentException
&)
729 OSL_FAIL( "LocalizationMgr::handleSetDefaultLocale: Invalid locale" );
732 // update locale toolbar
733 if (SfxBindings
* pBindings
= GetBindingsPtr())
734 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
737 void LocalizationMgr::handleSetCurrentLocale(const css::lang::Locale
& rLocale
)
739 if( !m_xStringResourceManager
.is() )
744 m_xStringResourceManager
->setCurrentLocale(rLocale
, false);
746 catch(const IllegalArgumentException
&)
748 OSL_FAIL( "LocalizationMgr::handleSetCurrentLocale: Invalid locale" );
751 // update locale toolbar
752 if (SfxBindings
* pBindings
= GetBindingsPtr())
753 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
755 if (DialogWindow
* pDlgWin
= dynamic_cast<DialogWindow
*>(m_pShell
->GetCurWindow()))
756 if (!pDlgWin
->IsSuspended())
757 pDlgWin
->GetEditor().UpdatePropertyBrowserDelayed();
760 void LocalizationMgr::handleBasicStarted()
762 if( m_xStringResourceManager
.is() )
763 m_aLocaleBeforeBasicStart
= m_xStringResourceManager
->getCurrentLocale();
766 void LocalizationMgr::handleBasicStopped()
770 if( m_xStringResourceManager
.is() )
771 m_xStringResourceManager
->setCurrentLocale( m_aLocaleBeforeBasicStart
, true );
773 catch(const IllegalArgumentException
&)
779 static DialogWindow
* FindDialogWindowForEditor( DlgEditor
const * pEditor
)
781 Shell::WindowTable
const& aWindowTable
= GetShell()->GetWindowTable();
782 for (auto const& window
: aWindowTable
)
784 BaseWindow
* pWin
= window
.second
;
785 if (!pWin
->IsSuspended())
786 if (DialogWindow
* pDlgWin
= dynamic_cast<DialogWindow
*>(pWin
))
788 if (&pDlgWin
->GetEditor() == pEditor
)
796 void LocalizationMgr::setControlResourceIDsForNewEditorObject( DlgEditor
const * pEditor
,
797 const Any
& rControlAny
, std::u16string_view aCtrlName
)
799 // Get library for DlgEditor
800 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
803 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
804 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::setControlResourceIDsForNewEditorObject: invalid document!" );
805 if ( !aDocument
.isValid() )
807 const OUString
& rLibName
= pDlgWin
->GetLibName();
808 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
809 Reference
< XStringResourceManager
> xStringResourceManager
=
810 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
812 // Set resource property
813 if( !xStringResourceManager
.is() || !xStringResourceManager
->getLocales().hasElements() )
816 OUString aDialogName
= pDlgWin
->GetName();
817 Reference
< XStringResourceResolver
> xDummyStringResolver
;
818 sal_Int32 nChangedCount
= implHandleControlResourceProperties
819 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
820 xDummyStringResolver
, SET_IDS
);
823 MarkDocumentModified( aDocument
);
826 void LocalizationMgr::renameControlResourceIDsForEditorObject( DlgEditor
const * pEditor
,
827 const css::uno::Any
& rControlAny
, std::u16string_view aNewCtrlName
)
829 // Get library for DlgEditor
830 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
833 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
834 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::renameControlResourceIDsForEditorObject: invalid document!" );
835 if ( !aDocument
.isValid() )
837 const OUString
& rLibName
= pDlgWin
->GetLibName();
838 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
839 Reference
< XStringResourceManager
> xStringResourceManager
=
840 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
842 // Set resource property
843 if( !xStringResourceManager
.is() || !xStringResourceManager
->getLocales().hasElements() )
846 OUString aDialogName
= pDlgWin
->GetName();
847 Reference
< XStringResourceResolver
> xDummyStringResolver
;
848 implHandleControlResourceProperties
849 ( rControlAny
, aDialogName
, aNewCtrlName
, xStringResourceManager
,
850 xDummyStringResolver
, RENAME_CONTROL_IDS
);
854 void LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( DlgEditor
const * pEditor
,
855 const Any
& rControlAny
, std::u16string_view aCtrlName
)
857 // Get library for DlgEditor
858 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
861 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
862 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject: invalid document!" );
863 if ( !aDocument
.isValid() )
865 const OUString
& rLibName
= pDlgWin
->GetLibName();
866 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
867 Reference
< XStringResourceManager
> xStringResourceManager
=
868 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
870 OUString aDialogName
= pDlgWin
->GetName();
871 Reference
< XStringResourceResolver
> xDummyStringResolver
;
872 sal_Int32 nChangedCount
= implHandleControlResourceProperties
873 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
874 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
877 MarkDocumentModified( aDocument
);
880 void LocalizationMgr::setStringResourceAtDialog( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
881 std::u16string_view aDlgName
, const Reference
< container::XNameContainer
>& xDialogModel
)
884 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
885 Reference
< XStringResourceManager
> xStringResourceManager
=
886 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
888 // Set resource property
889 if( !xStringResourceManager
.is() )
892 // Not very elegant as dialog may or may not be localized yet
893 // TODO: Find better place, where dialog is created
894 if( xStringResourceManager
->getLocales().hasElements() )
897 aDialogCtrl
<<= xDialogModel
;
898 Reference
< XStringResourceResolver
> xDummyStringResolver
;
899 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
900 std::u16string_view(), xStringResourceManager
,
901 xDummyStringResolver
, SET_IDS
);
904 Reference
< beans::XPropertySet
> xDlgPSet( xDialogModel
, UNO_QUERY
);
905 xDlgPSet
->setPropertyValue( "ResourceResolver", Any(xStringResourceManager
) );
908 void LocalizationMgr::renameStringResourceIDs( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
909 std::u16string_view aDlgName
, const Reference
< container::XNameContainer
>& xDialogModel
)
912 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
913 Reference
< XStringResourceManager
> xStringResourceManager
=
914 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
915 if( !xStringResourceManager
.is() )
919 aDialogCtrl
<<= xDialogModel
;
920 Reference
< XStringResourceResolver
> xDummyStringResolver
;
921 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
922 std::u16string_view(), xStringResourceManager
,
923 xDummyStringResolver
, RENAME_DIALOG_IDS
);
925 // Handle all controls
926 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
927 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
928 implHandleControlResourceProperties( aCtrl
, aDlgName
,
929 rCtrlName
, xStringResourceManager
,
930 xDummyStringResolver
, RENAME_DIALOG_IDS
);
934 void LocalizationMgr::removeResourceForDialog( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
935 std::u16string_view aDlgName
, const Reference
< container::XNameContainer
>& xDialogModel
)
938 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
939 Reference
< XStringResourceManager
> xStringResourceManager
=
940 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
941 if( !xStringResourceManager
.is() )
945 aDialogCtrl
<<= xDialogModel
;
946 Reference
< XStringResourceResolver
> xDummyStringResolver
;
947 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
948 std::u16string_view(), xStringResourceManager
,
949 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
951 // Handle all controls
952 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
953 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
954 implHandleControlResourceProperties( aCtrl
, aDlgName
,
955 rCtrlName
, xStringResourceManager
,
956 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
960 void LocalizationMgr::resetResourceForDialog( const Reference
< container::XNameContainer
>& xDialogModel
,
961 const Reference
< XStringResourceManager
>& xStringResourceManager
)
963 if( !xStringResourceManager
.is() )
967 std::u16string_view aDummyName
;
969 aDialogCtrl
<<= xDialogModel
;
970 Reference
< XStringResourceResolver
> xDummyStringResolver
;
971 implHandleControlResourceProperties( aDialogCtrl
, aDummyName
,
972 aDummyName
, xStringResourceManager
, xDummyStringResolver
, RESET_IDS
);
974 // Handle all controls
975 for(const auto& rCtrlName
: xDialogModel
->getElementNames()){
976 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
977 implHandleControlResourceProperties( aCtrl
, aDummyName
,
978 rCtrlName
, xStringResourceManager
, xDummyStringResolver
, RESET_IDS
);
982 void LocalizationMgr::setResourceIDsForDialog( const Reference
< container::XNameContainer
>& xDialogModel
,
983 const Reference
< XStringResourceManager
>& xStringResourceManager
)
985 if( !xStringResourceManager
.is() )
989 std::u16string_view aDummyName
;
991 aDialogCtrl
<<= xDialogModel
;
992 Reference
< XStringResourceResolver
> xDummyStringResolver
;
993 implHandleControlResourceProperties( aDialogCtrl
, aDummyName
,
994 aDummyName
, xStringResourceManager
, xDummyStringResolver
, SET_IDS
);
996 // Handle all controls
997 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
998 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
999 implHandleControlResourceProperties( aCtrl
, aDummyName
,
1000 rCtrlName
, xStringResourceManager
, xDummyStringResolver
, SET_IDS
);
1004 void LocalizationMgr::copyResourcesForPastedEditorObject( DlgEditor
const * pEditor
,
1005 const Any
& rControlAny
, std::u16string_view aCtrlName
,
1006 const Reference
< XStringResourceResolver
>& xSourceStringResolver
)
1008 // Get library for DlgEditor
1009 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
1012 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
1013 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::copyResourcesForPastedEditorObject: invalid document!" );
1014 if ( !aDocument
.isValid() )
1016 const OUString
& rLibName
= pDlgWin
->GetLibName();
1017 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
1018 Reference
< XStringResourceManager
> xStringResourceManager
=
1019 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
1021 // Set resource property
1022 if( !xStringResourceManager
.is() || !xStringResourceManager
->getLocales().hasElements() )
1025 OUString aDialogName
= pDlgWin
->GetName();
1026 implHandleControlResourceProperties
1027 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
1028 xSourceStringResolver
, MOVE_RESOURCES
);
1031 void LocalizationMgr::copyResourceForDroppedDialog( const Reference
< container::XNameContainer
>& xDialogModel
,
1032 std::u16string_view aDialogName
,
1033 const Reference
< XStringResourceManager
>& xStringResourceManager
,
1034 const Reference
< XStringResourceResolver
>& xSourceStringResolver
)
1036 if( !xStringResourceManager
.is() )
1039 // Dialog as control
1041 aDialogCtrl
<<= xDialogModel
;
1042 implHandleControlResourceProperties( aDialogCtrl
, aDialogName
,
1043 std::u16string_view(), xStringResourceManager
, xSourceStringResolver
, MOVE_RESOURCES
);
1045 // Handle all controls
1046 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
1047 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
1048 implHandleControlResourceProperties( aCtrl
, aDialogName
,
1049 rCtrlName
, xStringResourceManager
, xSourceStringResolver
, MOVE_RESOURCES
);
1053 void LocalizationMgr::copyResourceForDialog(
1054 const Reference
< container::XNameContainer
>& xDialogModel
,
1055 const Reference
< XStringResourceResolver
>& xSourceStringResolver
,
1056 const Reference
< XStringResourceManager
>& xTargetStringResourceManager
)
1058 if( !xDialogModel
.is() || !xSourceStringResolver
.is() || !xTargetStringResourceManager
.is() )
1061 std::u16string_view aDummyName
;
1063 aDialogCtrl
<<= xDialogModel
;
1064 implHandleControlResourceProperties
1065 ( aDialogCtrl
, aDummyName
, aDummyName
, xTargetStringResourceManager
,
1066 xSourceStringResolver
, COPY_RESOURCES
);
1068 // Handle all controls
1069 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
1070 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
1071 implHandleControlResourceProperties( aCtrl
, aDummyName
, aDummyName
,
1072 xTargetStringResourceManager
, xSourceStringResolver
, COPY_RESOURCES
);
1076 Reference
< XStringResourceManager
> LocalizationMgr::getStringResourceFromDialogLibrary
1077 ( const Reference
< container::XNameContainer
>& xDialogLib
)
1079 Reference
< XStringResourceManager
> xStringResourceManager
;
1080 if( xDialogLib
.is() )
1082 Reference
< resource::XStringResourceSupplier
> xStringResourceSupplier( xDialogLib
, UNO_QUERY
);
1083 if( xStringResourceSupplier
.is() )
1085 Reference
< resource::XStringResourceResolver
>
1086 xStringResourceResolver
= xStringResourceSupplier
->getStringResource();
1088 xStringResourceManager
=
1089 Reference
< resource::XStringResourceManager
>( xStringResourceResolver
, UNO_QUERY
);
1092 return xStringResourceManager
;
1095 } // namespace basctl
1097 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */