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 OUStringLiteral
aDot(u
".");
57 constexpr OUStringLiteral
aEsc(u
"&");
58 constexpr OUStringLiteral
aSemi(u
";");
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 OUStringLiteral aToolBarResName
= u
"private:resource/toolbar/translationbar";
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 OUString aPropIdStr
= aEsc
+ aPureIdStr
;
438 pIdStrings
[i
] = aPropIdStr
;
440 xPropertySet
->setPropertyValue( aPropName
, Any(aIdStrings
) );
442 // Replace id by string from StringResource
443 else if( eMode
== RESET_IDS
)
445 Sequence
< OUString
> aNewPropStrings
;
446 aNewPropStrings
.realloc( nPropStringCount
);
447 OUString
* pNewPropStrings
= aNewPropStrings
.getArray();
450 for ( i
= 0; i
< nPropStringCount
; ++i
)
452 OUString aIdStr
= pPropStrings
[i
];
453 OUString aNewPropStr
= aIdStr
;
454 if( aIdStr
.getLength() > 1 )
456 OUString aPureIdStr
= aIdStr
.copy( 1 );
459 aNewPropStr
= xStringResourceManager
->resolveString( aPureIdStr
);
461 catch(const MissingResourceException
&)
465 pNewPropStrings
[i
] = aNewPropStr
;
467 xPropertySet
->setPropertyValue( aPropName
, Any(aNewPropStrings
) );
469 // Remove Id for all locales
470 else if( eMode
== REMOVE_IDS_FROM_RESOURCE
)
472 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
474 for ( i
= 0; i
< nPropStringCount
; ++i
)
476 OUString aIdStr
= pPropStrings
[i
];
477 if( aIdStr
.getLength() > 1 )
479 OUString aPureIdStr
= aIdStr
.copy( 1 );
481 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
483 const Locale
& rLocale
= pLocales
[iLocale
];
486 xStringResourceManager
->removeIdForLocale( aPureIdStr
, rLocale
);
488 catch(const MissingResourceException
&)
495 // Rename resource id
496 else if( eMode
== RENAME_CONTROL_IDS
)
498 Sequence
< OUString
> aIdStrings
;
499 aIdStrings
.realloc( nPropStringCount
);
500 OUString
* pIdStrings
= aIdStrings
.getArray();
502 OUString aIdStrBase
= aDot
507 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
509 for ( i
= 0; i
< nPropStringCount
; ++i
)
511 OUString aSourceIdStr
= pPropStrings
[i
];
512 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
514 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
515 OUString aPureIdStr
= OUString::number( nUniqueId
)
518 // Set Id for all locales
519 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
521 const Locale
& rLocale
= pLocales
[ iLocale
];
525 OUString aResStr
= xStringResourceManager
->resolveStringForLocale
526 ( aPureSourceIdStr
, rLocale
);
527 xStringResourceManager
->removeIdForLocale( aPureSourceIdStr
, rLocale
);
528 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
530 catch(const MissingResourceException
&)
534 OUString aPropIdStr
= aEsc
+ aPureIdStr
;
535 pIdStrings
[i
] = aPropIdStr
;
537 xPropertySet
->setPropertyValue( aPropName
, Any(aIdStrings
) );
539 // Replace string by string from source StringResourceResolver
540 else if( eMode
== MOVE_RESOURCES
&& xSourceStringResolver
.is() )
542 Sequence
< OUString
> aIdStrings
;
543 aIdStrings
.realloc( nPropStringCount
);
544 OUString
* pIdStrings
= aIdStrings
.getArray();
546 OUString aIdStrBase
= aDot
551 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
553 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
555 for ( i
= 0; i
< nPropStringCount
; ++i
)
557 OUString aSourceIdStr
= pPropStrings
[i
];
558 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
560 sal_Int32 nUniqueId
= xStringResourceManager
->getUniqueNumericId();
561 OUString aPureIdStr
= OUString::number( nUniqueId
)
564 // Set Id for all locales
565 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
567 const Locale
& rLocale
= pLocales
[ iLocale
];
572 aResStr
= xSourceStringResolver
->resolveStringForLocale
573 ( aPureSourceIdStr
, rLocale
);
575 catch(const MissingResourceException
&)
577 aResStr
= xSourceStringResolver
->resolveStringForLocale
578 ( aPureSourceIdStr
, rDefaultLocale
);
580 xStringResourceManager
->setStringForLocale( aPureIdStr
, aResStr
, rLocale
);
583 OUString aPropIdStr
= aEsc
+ aPureIdStr
;
584 pIdStrings
[i
] = aPropIdStr
;
586 xPropertySet
->setPropertyValue( aPropName
, Any(aIdStrings
) );
588 // Copy string from source to target resource
589 else if( eMode
== COPY_RESOURCES
&& xSourceStringResolver
.is() )
591 const Locale
& rDefaultLocale
= xSourceStringResolver
->getDefaultLocale();
593 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
595 for ( i
= 0; i
< nPropStringCount
; ++i
)
597 OUString aSourceIdStr
= pPropStrings
[i
];
598 OUString aPureSourceIdStr
= aSourceIdStr
.copy( 1 );
600 // Set Id for all locales
601 for( sal_Int32 iLocale
= 0 ; iLocale
< nLocaleCount
; iLocale
++ )
603 const Locale
& rLocale
= pLocales
[ iLocale
];
608 aResStr
= xSourceStringResolver
->resolveStringForLocale
609 ( aPureSourceIdStr
, rLocale
);
611 catch(const MissingResourceException
&)
613 aResStr
= xSourceStringResolver
->resolveStringForLocale
614 ( aPureSourceIdStr
, rDefaultLocale
);
616 xStringResourceManager
->setStringForLocale( aPureSourceIdStr
, aResStr
, rLocale
);
625 return nChangedCount
;
629 void LocalizationMgr::handleAddLocales( const Sequence
< Locale
>& aLocaleSeq
)
631 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
632 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
634 if( isLibraryLocalized() )
636 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
638 const Locale
& rLocale
= pLocales
[ i
];
639 m_xStringResourceManager
->newLocale( rLocale
);
644 DBG_ASSERT( nLocaleCount
==1, "LocalizationMgr::handleAddLocales(): Only one first locale allowed" );
646 const Locale
& rLocale
= pLocales
[ 0 ];
647 m_xStringResourceManager
->newLocale( rLocale
);
648 enableResourceForAllLibraryDialogs();
651 MarkDocumentModified( m_aDocument
);
653 // update locale toolbar
654 if (SfxBindings
* pBindings
= GetBindingsPtr())
655 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
657 handleTranslationbar();
661 void LocalizationMgr::handleRemoveLocales( const Sequence
< Locale
>& aLocaleSeq
)
663 const Locale
* pLocales
= aLocaleSeq
.getConstArray();
664 sal_Int32 nLocaleCount
= aLocaleSeq
.getLength();
665 bool bConsistent
= true;
666 bool bModified
= false;
668 for( sal_Int32 i
= 0 ; i
< nLocaleCount
; i
++ )
670 const Locale
& rLocale
= pLocales
[ i
];
673 // Check if last locale
674 Sequence
< Locale
> aResLocaleSeq
= m_xStringResourceManager
->getLocales();
675 if( aResLocaleSeq
.getLength() == 1 )
677 const Locale
& rLastResLocale
= aResLocaleSeq
.getConstArray()[ 0 ];
678 if( localesAreEqual( rLocale
, rLastResLocale
) )
680 disableResourceForAllLibraryDialogs();
684 // Inconsistency, keep last locale
694 m_xStringResourceManager
->removeLocale( rLocale
);
697 catch(const IllegalArgumentException
&)
705 MarkDocumentModified( m_aDocument
);
708 if (SfxBindings
* pBindings
= GetBindingsPtr())
710 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
711 pBindings
->Invalidate( SID_BASICIDE_MANAGE_LANG
);
714 handleTranslationbar();
717 DBG_ASSERT( bConsistent
,
718 "LocalizationMgr::handleRemoveLocales(): sequence contains unsupported locales" );
721 void LocalizationMgr::handleSetDefaultLocale(const Locale
& rLocale
)
723 if( !m_xStringResourceManager
.is() )
728 m_xStringResourceManager
->setDefaultLocale(rLocale
);
730 catch(const IllegalArgumentException
&)
732 OSL_FAIL( "LocalizationMgr::handleSetDefaultLocale: Invalid locale" );
735 // update locale toolbar
736 if (SfxBindings
* pBindings
= GetBindingsPtr())
737 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
740 void LocalizationMgr::handleSetCurrentLocale(const css::lang::Locale
& rLocale
)
742 if( !m_xStringResourceManager
.is() )
747 m_xStringResourceManager
->setCurrentLocale(rLocale
, false);
749 catch(const IllegalArgumentException
&)
751 OSL_FAIL( "LocalizationMgr::handleSetCurrentLocale: Invalid locale" );
754 // update locale toolbar
755 if (SfxBindings
* pBindings
= GetBindingsPtr())
756 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
758 if (DialogWindow
* pDlgWin
= dynamic_cast<DialogWindow
*>(m_pShell
->GetCurWindow()))
759 if (!pDlgWin
->IsSuspended())
760 pDlgWin
->GetEditor().UpdatePropertyBrowserDelayed();
763 void LocalizationMgr::handleBasicStarted()
765 if( m_xStringResourceManager
.is() )
766 m_aLocaleBeforeBasicStart
= m_xStringResourceManager
->getCurrentLocale();
769 void LocalizationMgr::handleBasicStopped()
773 if( m_xStringResourceManager
.is() )
774 m_xStringResourceManager
->setCurrentLocale( m_aLocaleBeforeBasicStart
, true );
776 catch(const IllegalArgumentException
&)
782 static DialogWindow
* FindDialogWindowForEditor( DlgEditor
const * pEditor
)
784 Shell::WindowTable
const& aWindowTable
= GetShell()->GetWindowTable();
785 for (auto const& window
: aWindowTable
)
787 BaseWindow
* pWin
= window
.second
;
788 if (!pWin
->IsSuspended())
789 if (DialogWindow
* pDlgWin
= dynamic_cast<DialogWindow
*>(pWin
))
791 if (&pDlgWin
->GetEditor() == pEditor
)
799 void LocalizationMgr::setControlResourceIDsForNewEditorObject( DlgEditor
const * pEditor
,
800 const Any
& rControlAny
, std::u16string_view aCtrlName
)
802 // Get library for DlgEditor
803 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
806 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
807 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::setControlResourceIDsForNewEditorObject: invalid document!" );
808 if ( !aDocument
.isValid() )
810 const OUString
& rLibName
= pDlgWin
->GetLibName();
811 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
812 Reference
< XStringResourceManager
> xStringResourceManager
=
813 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
815 // Set resource property
816 if( !xStringResourceManager
.is() || !xStringResourceManager
->getLocales().hasElements() )
819 OUString aDialogName
= pDlgWin
->GetName();
820 Reference
< XStringResourceResolver
> xDummyStringResolver
;
821 sal_Int32 nChangedCount
= implHandleControlResourceProperties
822 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
823 xDummyStringResolver
, SET_IDS
);
826 MarkDocumentModified( aDocument
);
829 void LocalizationMgr::renameControlResourceIDsForEditorObject( DlgEditor
const * pEditor
,
830 const css::uno::Any
& rControlAny
, std::u16string_view aNewCtrlName
)
832 // Get library for DlgEditor
833 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
836 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
837 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::renameControlResourceIDsForEditorObject: invalid document!" );
838 if ( !aDocument
.isValid() )
840 const OUString
& rLibName
= pDlgWin
->GetLibName();
841 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
842 Reference
< XStringResourceManager
> xStringResourceManager
=
843 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
845 // Set resource property
846 if( !xStringResourceManager
.is() || !xStringResourceManager
->getLocales().hasElements() )
849 OUString aDialogName
= pDlgWin
->GetName();
850 Reference
< XStringResourceResolver
> xDummyStringResolver
;
851 implHandleControlResourceProperties
852 ( rControlAny
, aDialogName
, aNewCtrlName
, xStringResourceManager
,
853 xDummyStringResolver
, RENAME_CONTROL_IDS
);
857 void LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( DlgEditor
const * pEditor
,
858 const Any
& rControlAny
, std::u16string_view aCtrlName
)
860 // Get library for DlgEditor
861 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
864 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
865 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject: invalid document!" );
866 if ( !aDocument
.isValid() )
868 const OUString
& rLibName
= pDlgWin
->GetLibName();
869 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
870 Reference
< XStringResourceManager
> xStringResourceManager
=
871 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
873 OUString aDialogName
= pDlgWin
->GetName();
874 Reference
< XStringResourceResolver
> xDummyStringResolver
;
875 sal_Int32 nChangedCount
= implHandleControlResourceProperties
876 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
877 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
880 MarkDocumentModified( aDocument
);
883 void LocalizationMgr::setStringResourceAtDialog( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
884 std::u16string_view aDlgName
, const Reference
< container::XNameContainer
>& xDialogModel
)
887 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
888 Reference
< XStringResourceManager
> xStringResourceManager
=
889 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
891 // Set resource property
892 if( !xStringResourceManager
.is() )
895 // Not very elegant as dialog may or may not be localized yet
896 // TODO: Find better place, where dialog is created
897 if( xStringResourceManager
->getLocales().hasElements() )
900 aDialogCtrl
<<= xDialogModel
;
901 Reference
< XStringResourceResolver
> xDummyStringResolver
;
902 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
903 std::u16string_view(), xStringResourceManager
,
904 xDummyStringResolver
, SET_IDS
);
907 Reference
< beans::XPropertySet
> xDlgPSet( xDialogModel
, UNO_QUERY
);
908 xDlgPSet
->setPropertyValue( "ResourceResolver", Any(xStringResourceManager
) );
911 void LocalizationMgr::renameStringResourceIDs( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
912 std::u16string_view aDlgName
, const Reference
< container::XNameContainer
>& xDialogModel
)
915 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
916 Reference
< XStringResourceManager
> xStringResourceManager
=
917 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
918 if( !xStringResourceManager
.is() )
922 aDialogCtrl
<<= xDialogModel
;
923 Reference
< XStringResourceResolver
> xDummyStringResolver
;
924 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
925 std::u16string_view(), xStringResourceManager
,
926 xDummyStringResolver
, RENAME_DIALOG_IDS
);
928 // Handle all controls
929 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
930 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
931 implHandleControlResourceProperties( aCtrl
, aDlgName
,
932 rCtrlName
, xStringResourceManager
,
933 xDummyStringResolver
, RENAME_DIALOG_IDS
);
937 void LocalizationMgr::removeResourceForDialog( const ScriptDocument
& rDocument
, const OUString
& aLibName
,
938 std::u16string_view aDlgName
, const Reference
< container::XNameContainer
>& xDialogModel
)
941 Reference
< container::XNameContainer
> xDialogLib( rDocument
.getLibrary( E_DIALOGS
, aLibName
, true ) );
942 Reference
< XStringResourceManager
> xStringResourceManager
=
943 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
944 if( !xStringResourceManager
.is() )
948 aDialogCtrl
<<= xDialogModel
;
949 Reference
< XStringResourceResolver
> xDummyStringResolver
;
950 implHandleControlResourceProperties( aDialogCtrl
, aDlgName
,
951 std::u16string_view(), xStringResourceManager
,
952 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
954 // Handle all controls
955 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
956 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
957 implHandleControlResourceProperties( aCtrl
, aDlgName
,
958 rCtrlName
, xStringResourceManager
,
959 xDummyStringResolver
, REMOVE_IDS_FROM_RESOURCE
);
963 void LocalizationMgr::resetResourceForDialog( const Reference
< container::XNameContainer
>& xDialogModel
,
964 const Reference
< XStringResourceManager
>& xStringResourceManager
)
966 if( !xStringResourceManager
.is() )
970 std::u16string_view aDummyName
;
972 aDialogCtrl
<<= xDialogModel
;
973 Reference
< XStringResourceResolver
> xDummyStringResolver
;
974 implHandleControlResourceProperties( aDialogCtrl
, aDummyName
,
975 aDummyName
, xStringResourceManager
, xDummyStringResolver
, RESET_IDS
);
977 // Handle all controls
978 for(const auto& rCtrlName
: xDialogModel
->getElementNames()){
979 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
980 implHandleControlResourceProperties( aCtrl
, aDummyName
,
981 rCtrlName
, xStringResourceManager
, xDummyStringResolver
, RESET_IDS
);
985 void LocalizationMgr::setResourceIDsForDialog( const Reference
< container::XNameContainer
>& xDialogModel
,
986 const Reference
< XStringResourceManager
>& xStringResourceManager
)
988 if( !xStringResourceManager
.is() )
992 std::u16string_view aDummyName
;
994 aDialogCtrl
<<= xDialogModel
;
995 Reference
< XStringResourceResolver
> xDummyStringResolver
;
996 implHandleControlResourceProperties( aDialogCtrl
, aDummyName
,
997 aDummyName
, xStringResourceManager
, xDummyStringResolver
, SET_IDS
);
999 // Handle all controls
1000 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
1001 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
1002 implHandleControlResourceProperties( aCtrl
, aDummyName
,
1003 rCtrlName
, xStringResourceManager
, xDummyStringResolver
, SET_IDS
);
1007 void LocalizationMgr::copyResourcesForPastedEditorObject( DlgEditor
const * pEditor
,
1008 const Any
& rControlAny
, std::u16string_view aCtrlName
,
1009 const Reference
< XStringResourceResolver
>& xSourceStringResolver
)
1011 // Get library for DlgEditor
1012 DialogWindow
* pDlgWin
= FindDialogWindowForEditor( pEditor
);
1015 ScriptDocument
aDocument( pDlgWin
->GetDocument() );
1016 DBG_ASSERT( aDocument
.isValid(), "LocalizationMgr::copyResourcesForPastedEditorObject: invalid document!" );
1017 if ( !aDocument
.isValid() )
1019 const OUString
& rLibName
= pDlgWin
->GetLibName();
1020 Reference
< container::XNameContainer
> xDialogLib( aDocument
.getLibrary( E_DIALOGS
, rLibName
, true ) );
1021 Reference
< XStringResourceManager
> xStringResourceManager
=
1022 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib
);
1024 // Set resource property
1025 if( !xStringResourceManager
.is() || !xStringResourceManager
->getLocales().hasElements() )
1028 OUString aDialogName
= pDlgWin
->GetName();
1029 implHandleControlResourceProperties
1030 ( rControlAny
, aDialogName
, aCtrlName
, xStringResourceManager
,
1031 xSourceStringResolver
, MOVE_RESOURCES
);
1034 void LocalizationMgr::copyResourceForDroppedDialog( const Reference
< container::XNameContainer
>& xDialogModel
,
1035 std::u16string_view aDialogName
,
1036 const Reference
< XStringResourceManager
>& xStringResourceManager
,
1037 const Reference
< XStringResourceResolver
>& xSourceStringResolver
)
1039 if( !xStringResourceManager
.is() )
1042 // Dialog as control
1044 aDialogCtrl
<<= xDialogModel
;
1045 implHandleControlResourceProperties( aDialogCtrl
, aDialogName
,
1046 std::u16string_view(), xStringResourceManager
, xSourceStringResolver
, MOVE_RESOURCES
);
1048 // Handle all controls
1049 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
1050 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
1051 implHandleControlResourceProperties( aCtrl
, aDialogName
,
1052 rCtrlName
, xStringResourceManager
, xSourceStringResolver
, MOVE_RESOURCES
);
1056 void LocalizationMgr::copyResourceForDialog(
1057 const Reference
< container::XNameContainer
>& xDialogModel
,
1058 const Reference
< XStringResourceResolver
>& xSourceStringResolver
,
1059 const Reference
< XStringResourceManager
>& xTargetStringResourceManager
)
1061 if( !xDialogModel
.is() || !xSourceStringResolver
.is() || !xTargetStringResourceManager
.is() )
1064 std::u16string_view aDummyName
;
1066 aDialogCtrl
<<= xDialogModel
;
1067 implHandleControlResourceProperties
1068 ( aDialogCtrl
, aDummyName
, aDummyName
, xTargetStringResourceManager
,
1069 xSourceStringResolver
, COPY_RESOURCES
);
1071 // Handle all controls
1072 for(const auto& rCtrlName
: xDialogModel
->getElementNames()) {
1073 Any aCtrl
= xDialogModel
->getByName( rCtrlName
);
1074 implHandleControlResourceProperties( aCtrl
, aDummyName
, aDummyName
,
1075 xTargetStringResourceManager
, xSourceStringResolver
, COPY_RESOURCES
);
1079 Reference
< XStringResourceManager
> LocalizationMgr::getStringResourceFromDialogLibrary
1080 ( const Reference
< container::XNameContainer
>& xDialogLib
)
1082 Reference
< XStringResourceManager
> xStringResourceManager
;
1083 if( xDialogLib
.is() )
1085 Reference
< resource::XStringResourceSupplier
> xStringResourceSupplier( xDialogLib
, UNO_QUERY
);
1086 if( xStringResourceSupplier
.is() )
1088 Reference
< resource::XStringResourceResolver
>
1089 xStringResourceResolver
= xStringResourceSupplier
->getStringResource();
1091 xStringResourceManager
=
1092 Reference
< resource::XStringResourceManager
>( xStringResourceResolver
, UNO_QUERY
);
1095 return xStringResourceManager
;
1098 } // namespace basctl
1100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */