• <i id='I7VJh'><tr id='I7VJh'><dt id='I7VJh'><q id='I7VJh'><span id='I7VJh'><b id='I7VJh'><form id='I7VJh'><ins id='I7VJh'></ins><ul id='I7VJh'></ul><sub id='I7VJh'></sub></form><legend id='I7VJh'></legend><bdo id='I7VJh'><pre id='I7VJh'><center id='I7VJh'></center></pre></bdo></b><th id='I7VJh'></th></span></q></dt></tr></i><div id='I7VJh'><tfoot id='I7VJh'></tfoot><dl id='I7VJh'><fieldset id='I7VJh'></fieldset></dl></div>
  • <tfoot id='I7VJh'></tfoot>

      <legend id='I7VJh'><style id='I7VJh'><dir id='I7VJh'><q id='I7VJh'></q></dir></style></legend>

          <bdo id='I7VJh'></bdo><ul id='I7VJh'></ul>

        <small id='I7VJh'></small><noframes id='I7VJh'>

      1. 在 Java 中,我如何找出我的资源包可用的语言

        时间:2024-08-25

          1. <legend id='oQu2K'><style id='oQu2K'><dir id='oQu2K'><q id='oQu2K'></q></dir></style></legend>

              <bdo id='oQu2K'></bdo><ul id='oQu2K'></ul>
              <tfoot id='oQu2K'></tfoot>
                <tbody id='oQu2K'></tbody>
              <i id='oQu2K'><tr id='oQu2K'><dt id='oQu2K'><q id='oQu2K'><span id='oQu2K'><b id='oQu2K'><form id='oQu2K'><ins id='oQu2K'></ins><ul id='oQu2K'></ul><sub id='oQu2K'></sub></form><legend id='oQu2K'></legend><bdo id='oQu2K'><pre id='oQu2K'><center id='oQu2K'></center></pre></bdo></b><th id='oQu2K'></th></span></q></dt></tr></i><div id='oQu2K'><tfoot id='oQu2K'></tfoot><dl id='oQu2K'><fieldset id='oQu2K'></fieldset></dl></div>

                <small id='oQu2K'></small><noframes id='oQu2K'>

                • 本文介绍了在 Java 中,我如何找出我的资源包可用的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的主 jar 中打包了一些资源包

                  I have some resource bundles packaged in my main jar

                  widget_en.properties
                  widget_de.properties
                  

                  我根据我的默认语言环境检索资源包,如下所示

                  I retrieve a resource bundle based on my default locale as folows

                  ResourceBundle.getBundle("widget", Locale.getDefault());
                  

                  但我想向用户展示支持的可用语言列表,以便可以选择可能与其计算机默认设置不同的语言

                  But I want to present the user with a list of available languages supported so that can select a language that may be different to their computers default

                  但我在 ResourceBundle 中找不到列出可用语言环境的方法,我不想硬编码列表,因为添加另一个资源包时我可能会忘记更新它.

                  But I can't find a method in ResourceBundle that would list available locales, I don't want to hardcode a list as I may forget to update it when another resource bundle is added.

                  编辑

                  因为我只有不同语言的资源包(我没有国家细化),所以我通过遍历所有已知的语言代码生成了一个列表,并将每个语言代码检查为资源.

                  As I only resource bundles for different language (I dont have country refinements) so I have got generated a list by iterating through all the known language codes and check for each one as a resource.

                  String[]langs = Locale.getISOLanguages();
                  for(String lang:langs)
                  {
                        URL rb = ClassLoader.getSystemResource("widget_"+lang+".properties");
                        if(rb!=null)
                        {
                              System.out.println("Found:"+rb.toString());
                        }
                  }
                  

                  推荐答案

                  我不认为有一个 API,因为可以动态创建新的有效语言环境对象:

                  I don't think there is an API for this because new valid locale objects can be created on the fly:

                  Locale locale = new Locale("abcd");
                  

                  无需在某处注册.然后你可以使用资源包widget_abcd.properties 无限制:

                  without the need to register it somewhere. And then you can use a resource bundle widget_abcd.properties without restrictions:

                  ResourceBundle resource= ResourceBundle.getBundle("widget", new Locale("abcd"));
                  

                  来自 java.util.Locale API 文档:

                  From the java.util.Locale API docs:

                  因为 Locale 对象只是一个区域的标识符,所以没有当你构建一个 Locale 时执行有效性检查.如果你想查看特定资源是否可用于您的语言环境构造,你必须查询那些资源.

                  Because a Locale object is just an identifier for a region, no validity check is performed when you construct a Locale. If you want to see whether particular resources are available for the Locale you construct, you must query those resources.

                  要解决这个问题,您仍然可以遍历资源目录中所有名为widget_"的文件并发现新添加的资源包.

                  To solve the problem you can still iterate over all files called "widget_" in the resource directory and discover the new added resource bundles.

                  请注意,由于上述原因,Locale.getAvailableLocales() 不是 100% 确定:您可能有一天会定义非标准语言环境.但是,如果您只添加几个标准语言环境,则可以使用此静态方法迭代系统语言环境并获取相应的包.

                  Note that Locale.getAvailableLocales() is not 100% sure for the above reason: you might some day define a non standard locale. But if you'll add only a few standard locales you can use this static method to iterate over the system locales and get the corresponding bundles.

                  这篇关于在 Java 中,我如何找出我的资源包可用的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Java 应用程序中处理多种语言? 下一篇:如果您有 ISO 国家代码 `US`、`FR`,如何获得区域设置代码(`Locale.US`、`Locale.FRANC

                  相关文章

                • <small id='eYzRQ'></small><noframes id='eYzRQ'>

                    <bdo id='eYzRQ'></bdo><ul id='eYzRQ'></ul>
                  <legend id='eYzRQ'><style id='eYzRQ'><dir id='eYzRQ'><q id='eYzRQ'></q></dir></style></legend>
                  <tfoot id='eYzRQ'></tfoot>

                    1. <i id='eYzRQ'><tr id='eYzRQ'><dt id='eYzRQ'><q id='eYzRQ'><span id='eYzRQ'><b id='eYzRQ'><form id='eYzRQ'><ins id='eYzRQ'></ins><ul id='eYzRQ'></ul><sub id='eYzRQ'></sub></form><legend id='eYzRQ'></legend><bdo id='eYzRQ'><pre id='eYzRQ'><center id='eYzRQ'></center></pre></bdo></b><th id='eYzRQ'></th></span></q></dt></tr></i><div id='eYzRQ'><tfoot id='eYzRQ'></tfoot><dl id='eYzRQ'><fieldset id='eYzRQ'></fieldset></dl></div>