Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4829

Set a global resource bundle in class

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.5.12
    • Fix Version/s: 2.5.13
    • Component/s: Core
    • Labels:
      None

      Description

      In struts 2.5.12 the LocalizedTextUtil is removed.

      We used to `LocalizedTextUtil.addDefaultResourceBundle` in our classes to add resource bundles. This was mentioned in https://struts.apache.org/docs/how-do-i-set-a-global-resource-bundle.html

      Can you please let me know how this can be done with this change ?!

        Activity

        Hide
        lukaszlenart Lukasz Lenart added a comment -

        The best option is to use struts.custom.i18n.resources but if you need to do it programmatically you will have to options:

        • inject the LocalizedTextProvider into an action and add the resource bundles
        • implement your own version of the LocalizedTextProvider and register as it was shown here in Using only global bundles
        Show
        lukaszlenart Lukasz Lenart added a comment - The best option is to use struts.custom.i18n.resources but if you need to do it programmatically you will have to options: inject the LocalizedTextProvider into an action and add the resource bundles implement your own version of the LocalizedTextProvider and register as it was shown here in Using only global bundles
        Hide
        afattahi Alireza Fattahi added a comment - - edited

        Thanks !
        I put some code at WW-4830 for whom has same issue.

        Show
        afattahi Alireza Fattahi added a comment - - edited Thanks ! I put some code at WW-4830 for whom has same issue.
        Hide
        afattahi Alireza Fattahi added a comment - - edited

        Can you please help me with this too:

        Here is the code:

        public class CustomStrutsTextProviderFactory extends StrutsTextProviderFactory {
        	
        	@Override
        	protected TextProvider getTextProvider(Class clazz) {
                       localizedTextProvider.addDefaultResourceBundle("messages/label");
        		localizedTextProvider.addDefaultResourceBundle("messages/customerA/label");
        		return new CustomTextProvider(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
              }
        
        

        The code works, but for every request the CustomStrutsTextProviderFactory.getTextProvider is called. So the localizedTextProvider.addDefaultResourceBundle is called for every request. Although the AbstractLocalizedTextProvider.addDefaultResourceBundle make sure bundle is not doesn't get added more than once it is not a good approch ...

        I think the CustomStrutsTextProviderFactory could be write better, any idea!

        Show
        afattahi Alireza Fattahi added a comment - - edited Can you please help me with this too: Here is the code: public class CustomStrutsTextProviderFactory extends StrutsTextProviderFactory { @Override protected TextProvider getTextProvider( Class clazz) { localizedTextProvider.addDefaultResourceBundle( "messages/label" ); localizedTextProvider.addDefaultResourceBundle( "messages/customerA/label" ); return new CustomTextProvider(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider); } The code works, but for every request the CustomStrutsTextProviderFactory.getTextProvider is called. So the localizedTextProvider.addDefaultResourceBundle is called for every request. Although the AbstractLocalizedTextProvider.addDefaultResourceBundle make sure bundle is not doesn't get added more than once it is not a good approch ... I think the CustomStrutsTextProviderFactory could be write better, any idea!
        Hide
        lukaszlenart Lukasz Lenart added a comment - - edited

        I would do it like this

        public class MyTextProviderFactory implements TextProviderFactory {
        
            protected LocaleProviderFactory localeProviderFactory;
            protected LocalizedTextProvider localizedTextProvider;
            
            @Inject
            public MyTextProviderFactory(LocaleProviderFactory localeProviderFactory, LocalizedTextProvider localizedTextProvider) {
                this.localeProviderFactory = localeProviderFactory;
                this.localizedTextProvider = localizedTextProvider;
                
                this.localizedTextProvider.addDefaultResourceBundle("myBundle");
            }
        
            @Override
            public TextProvider createInstance(Class clazz) {
                TextProvider instance = getTextProvider(clazz);
                if (instance instanceof ResourceBundleTextProvider) {
                    ((ResourceBundleTextProvider) instance).setClazz(clazz);
                    ((ResourceBundleTextProvider) instance).setLocaleProvider(localeProviderFactory.createLocaleProvider());
                }
                return instance;
            }
        
            @Override
            public TextProvider createInstance(ResourceBundle bundle) {
                TextProvider instance = getTextProvider(bundle);
                if (instance instanceof ResourceBundleTextProvider) {
                    ((ResourceBundleTextProvider) instance).setBundle(bundle);
                    ((ResourceBundleTextProvider) instance).setLocaleProvider(localeProviderFactory.createLocaleProvider());
                }
                return instance;
            }
        
            protected TextProvider getTextProvider(Class clazz) {
                return new TextProviderSupport(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
            }
        
            protected TextProvider getTextProvider(ResourceBundle bundle) {
                return new TextProviderSupport(bundle, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
            }
        
        }
        
        Show
        lukaszlenart Lukasz Lenart added a comment - - edited I would do it like this public class MyTextProviderFactory implements TextProviderFactory { protected LocaleProviderFactory localeProviderFactory; protected LocalizedTextProvider localizedTextProvider; @Inject public MyTextProviderFactory(LocaleProviderFactory localeProviderFactory, LocalizedTextProvider localizedTextProvider) { this .localeProviderFactory = localeProviderFactory; this .localizedTextProvider = localizedTextProvider; this .localizedTextProvider.addDefaultResourceBundle( "myBundle" ); } @Override public TextProvider createInstance( Class clazz) { TextProvider instance = getTextProvider(clazz); if (instance instanceof ResourceBundleTextProvider) { ((ResourceBundleTextProvider) instance).setClazz(clazz); ((ResourceBundleTextProvider) instance).setLocaleProvider(localeProviderFactory.createLocaleProvider()); } return instance; } @Override public TextProvider createInstance(ResourceBundle bundle) { TextProvider instance = getTextProvider(bundle); if (instance instanceof ResourceBundleTextProvider) { ((ResourceBundleTextProvider) instance).setBundle(bundle); ((ResourceBundleTextProvider) instance).setLocaleProvider(localeProviderFactory.createLocaleProvider()); } return instance; } protected TextProvider getTextProvider( Class clazz) { return new TextProviderSupport(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider); } protected TextProvider getTextProvider(ResourceBundle bundle) { return new TextProviderSupport(bundle, localeProviderFactory.createLocaleProvider(), localizedTextProvider); } }

          People

          • Assignee:
            Unassigned
            Reporter:
            afattahi Alireza Fattahi
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development