Re: Dynamic translation of a plug-in

by enkonyito » Tue Jan 16, 2024 11:31 pm

Thanks to both of you for the information, it works for the photo creation panel (title, components).

Based on the ResourceAction class, dynamic translation works for plug-in actions, but the menu remains unchanged despite the new value.

Code: Select all

  public SimplePhotoRenderingAction(PhotoVideoRendering photoVideoRendering) {
    // modification start PVR-2.7
    ResourceBundle resource = ResourceBundle.getBundle("sh3dPlugin.ApplicationPlugin", Locale.getDefault(), getClass().getClassLoader());
    putPropertyValue(Property.NAME, photoVideoRendering.getUserPreferences().getLocalizedString(HomePane.class, "CREATE_PHOTO.Name")
        + " (PVR-" + photoVideoRendering.getVersion() + ")");
    putPropertyValue(Property.SHORT_DESCRIPTION, photoVideoRendering.getUserPreferences().getLocalizedString(HomePane.class, "CREATE_PHOTO.ShortDescription"));
    putPropertyValue(Property.MENU, resource.getString("SimplePhotoRenderingAction.MENU"));
    setEnabled(true);

    SimplePhotoRenderingAction.photoVideoRenderingPlugin = photoVideoRendering;

    photoVideoRendering.getUserPreferences().addPropertyChangeListener(UserPreferences.Property.LANGUAGE, new LanguageChangeListener(this));
    // modification end PVR-2.7
  }

Code: Select all

  // addition start PVR-2.7
  // Based on https://sourceforge.net/p/sweethome3d/code/8462/tree/trunk/SweetHome3D/src/com/eteks/sweethome3d/swing/ResourceAction.java#l89 (SH3D-7.1)
  /**
   * Preferences property listener bound to this action with a weak reference to avoid
   * strong link between preferences and this action.
   */
  private static class LanguageChangeListener implements PropertyChangeListener {
    private final WeakReference<SimplePhotoRenderingAction> pluginAction;
    public LanguageChangeListener(SimplePhotoRenderingAction pluginAction) {
      this.pluginAction = new WeakReference<SimplePhotoRenderingAction>(pluginAction);
    }

    public void propertyChange(PropertyChangeEvent ev) {
      // If action was garbage collected, remove this listener from preferences
      SimplePhotoRenderingAction pluginAction = this.pluginAction.get();
      if (pluginAction == null) {
         ((UserPreferences)ev.getSource()).removePropertyChangeListener(
             UserPreferences.Property.LANGUAGE, this);
      } else {
        ResourceBundle resource = ResourceBundle.getBundle("sh3dPlugin.ApplicationPlugin", Locale.getDefault(), getClass().getClassLoader());
        pluginAction.putPropertyValue(Property.NAME, ((UserPreferences)ev.getSource()).getLocalizedString(HomePane.class, "CREATE_PHOTO.Name")
            + " (PVR-" + SimplePhotoRenderingAction.photoVideoRenderingPlugin.getVersion() + ")");
        pluginAction.putPropertyValue(Property.SHORT_DESCRIPTION, ((UserPreferences)ev.getSource()).getLocalizedString(HomePane.class, "CREATE_PHOTO.ShortDescription"));
        pluginAction.putPropertyValue(Property.MENU, resource.getString("SimplePhotoRenderingAction.MENU"));
      }
    }
  }
  // addition end PVR-2.7
I could have put the plug-in actions in the existing [font=courier new]3D View[/font] menu, but I prefer to avoid doing so so that there is no confusion with the default actions of Sweet Home 3D.