Skip to content

Commit 750bb02

Browse files
committed
refactor to make localization an own annotation
1 parent aad9c04 commit 750bb02

File tree

3 files changed

+62
-21
lines changed

3 files changed

+62
-21
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.cryptomator.integrations.common;
22

3+
import org.jetbrains.annotations.ApiStatus;
4+
35
import java.lang.annotation.Documented;
46
import java.lang.annotation.ElementType;
57
import java.lang.annotation.Retention;
68
import java.lang.annotation.RetentionPolicy;
79
import java.lang.annotation.Target;
810

9-
import org.jetbrains.annotations.ApiStatus;
10-
1111
/**
1212
* A humanreadable name of the annotated class.
1313
*/
@@ -17,5 +17,4 @@
1717
@ApiStatus.Experimental
1818
public @interface DisplayName {
1919
String value();
20-
2120
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.cryptomator.integrations.common;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
5+
import java.lang.annotation.Documented;
6+
import java.lang.annotation.ElementType;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
* A humanreadable, localized name of the annotated class.
13+
*/
14+
@Documented
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Target(ElementType.TYPE)
17+
@ApiStatus.Experimental
18+
public @interface LocalizedDisplayName {
19+
20+
/**
21+
* Name of the localization bundle, where the display name is loaded from.
22+
*
23+
* @return Name of the localization bundle
24+
*/
25+
String bundle();
26+
27+
/**
28+
* The localization key containing the display name.
29+
*
30+
* @return Localization key to use
31+
*/
32+
String key();
33+
34+
}
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package org.cryptomator.integrations.common;
22

3+
import java.util.ResourceBundle;
4+
35
/**
4-
* A service provider with a specific, human-readable name.
5-
*
6+
* A service provider with a human-readable, possibly localized name.
67
*/
78
public interface NamedServiceProvider {
8-
9-
/**
10-
* Get the name of this service provider.
11-
* @implNote The default implementation looks for the {@link DisplayName} annotation and uses its value. If the annotation is not present, it falls back to the qualified class name.
12-
* @return The name of the service provider
13-
*
14-
* @see DisplayName
15-
*/
16-
default public String getName() {
17-
var displayName = this.getClass().getAnnotation(DisplayName.class);
18-
if(displayName != null) {
19-
return displayName.value();
20-
} else {
21-
return this.getClass().getName();
22-
}
23-
}
9+
10+
/**
11+
* Get the name of this service provider.
12+
*
13+
* @return The name of the service provider
14+
* @implNote The default implementation looks first for a {@link LocalizedDisplayName} and loads the name from the specified resource bundle/key. If the annotation is not present, the code looks for {@link DisplayName} and uses its value. If none of the former annotations are present, it falls back to the qualified class name.
15+
* @see DisplayName
16+
* @see LocalizedDisplayName
17+
*/
18+
default String getName() {
19+
var localizedDisplayName = this.getClass().getAnnotation(LocalizedDisplayName.class);
20+
if (localizedDisplayName != null) {
21+
return ResourceBundle.getBundle(localizedDisplayName.bundle()) //
22+
.getString(localizedDisplayName.key());
23+
}
24+
25+
var displayName = this.getClass().getAnnotation(DisplayName.class);
26+
if (displayName != null) {
27+
return displayName.value();
28+
} else {
29+
return this.getClass().getName();
30+
}
31+
}
2432
}

0 commit comments

Comments
 (0)