classlib: add ResourceBundle.getBaseBundleName

This commit is contained in:
Alexey Andreev 2022-05-08 18:07:40 +03:00
parent 500d72d596
commit e625409562
5 changed files with 59 additions and 2 deletions

View File

@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
@ -58,7 +59,8 @@ public class ResourceBundleImpl {
Set<String> implementations = new LinkedHashSet<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(),
StandardCharsets.UTF_8))) {
while (true) {
String line = reader.readLine();
if (line == null) {

View File

@ -51,6 +51,8 @@ public abstract class TResourceBundle {
private static final Map<String, Supplier<ResourceBundle>> bundleProviders =
ResourceBundleImpl.createBundleMap(false);
private String name;
public TResourceBundle() {
}
@ -118,6 +120,10 @@ public abstract class TResourceBundle {
return locale;
}
public String getBaseBundleName() {
return name;
}
public final Object getObject(String key) {
TResourceBundle last;
TResourceBundle theParent = this;
@ -173,6 +179,7 @@ public abstract class TResourceBundle {
}
}
cache.put(bundleName, bundle);
bundle.name = base;
return bundle;
}
@ -180,6 +187,7 @@ public abstract class TResourceBundle {
bundle = handleGetBundle(base, extension, loadBase);
if (bundle != null) {
cache.put(bundleName, bundle);
bundle.name = base;
return bundle;
}
}
@ -219,7 +227,7 @@ public abstract class TResourceBundle {
}
country = name.substring(index + 1, nextIndex);
if (nextIndex + 1 < name.length()) {
variant = name.substring(nextIndex + 1, name.length());
variant = name.substring(nextIndex + 1);
}
}
}

View File

@ -72,6 +72,19 @@ public class ResourceBundleTest {
}
@Test
public void simpleBundle() {
ResourceBundle bundle = ResourceBundle.getBundle("testBundle", Locale.ENGLISH);
assertEquals("testBundle", bundle.getBaseBundleName());
assertEquals("Test passed", bundle.getString("a"));
try {
bundle.getString("b");
fail("MissingResourceException not thrown");
} catch (MissingResourceException e) {
assertEquals("b", e.getKey());
}
}
/*
* the class and constructor must be public so ResourceBundle has the
* possibility to instantiate

View File

@ -0,0 +1,17 @@
#
# Copyright 2022 Alexey Andreev.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
testBundle_en

View File

@ -0,0 +1,17 @@
#
# Copyright 2022 Alexey Andreev.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
a=Test passed