数据库支持i18n for java web-app

ScA*_*er2 6 java internationalization

我想使用数据库来存储i18n键/值对,这样我们就可以在运行时修改/重新加载i18n数据.有没有人这样做过?或者有没有人知道如何实现这个?我已经阅读了几个线程,但我还没有看到一个可行的解决方案.

我特别指的是可以使用jstl标签的东西,例如

<fmt:setlocale>
<fmt:bundle>
<fmt:setBundle>
<fmt:message>
Run Code Online (Sandbox Code Playgroud)

我认为这将涉及扩展ResourceBundle,但是当我尝试这个时,我遇到的问题与jstl标签获取资源包的方式有关.

ScA*_*er2 13

我终于得到了danb的帮助.

这是我的资源包类和资源包控件类.

我使用了来自@ [danb]的代码.

ResourceBundle bundle = ResourceBundle.getBundle("AwesomeBundle", locale, DbResourceBundle.getMyControl());
javax.servlet.jsp.jstl.core.Config.set(actionBeanContext.getRequest(), Config.FMT_LOCALIZATION_CONTEXT, new LocalizationContext(bundle, locale));
Run Code Online (Sandbox Code Playgroud)

并写了这堂课.

public class DbResourceBundle extends ResourceBundle
{
    private Properties properties;

    public DbResourceBundle(Properties inProperties)
    {
        properties = inProperties;
    }

    @Override
    @SuppressWarnings(value = { "unchecked" })
    public Enumeration<String> getKeys()
    {
        return properties != null ? ((Enumeration<String>) properties.propertyNames()) : null;
    }

    @Override
    protected Object handleGetObject(String key)
    {
        return properties.getProperty(key);
    }

    public static ResourceBundle.Control getMyControl()
    {
        return new ResourceBundle.Control()
        {

            @Override
            public List<String> getFormats(String baseName)
            {
                if (baseName == null)
                {
                    throw new NullPointerException();
                }
                return Arrays.asList("db");
            }

            @Override
            public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException,
                  InstantiationException, IOException
            {
                if ((baseName == null) || (locale == null) || (format == null) || (loader == null))
                    throw new NullPointerException();
                ResourceBundle bundle = null;
                if (format.equals("db"))
                {
                    Properties p = new Properties();
                    DataSource ds = (DataSource) ContextFactory.getApplicationContext().getBean("clinicalDataSource");
                    Connection con = null;
                    Statement s = null;
                    ResultSet rs = null;
                    try
                    {
                        con = ds.getConnection();
                        StringBuilder query = new StringBuilder();
                        query.append("select label, value from i18n where bundle='" + StringEscapeUtils.escapeSql(baseName) + "' ");

                        if (locale != null)
                        {
                            if (StringUtils.isNotBlank(locale.getCountry()))
                            {
                                query.append("and country='" + escapeSql(locale.getCountry()) + "' ");

                            }
                            if (StringUtils.isNotBlank(locale.getLanguage()))
                            {
                                query.append("and language='" + escapeSql(locale.getLanguage()) + "' ");

                            }
                            if (StringUtils.isNotBlank(locale.getVariant()))
                            {
                                query.append("and variant='" + escapeSql(locale.getVariant()) + "' ");

                            }
                        }
                        s = con.createStatement();
                        rs = s.executeQuery(query.toString());
                        while (rs.next())
                        {
                            p.setProperty(rs.getString(1), rs.getString(2));
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        throw new RuntimeException("Can not build properties: " + e);
                    }
                    finally
                    {
                        DbUtils.closeQuietly(con, s, rs);
                    }
                    bundle = new DbResourceBundle(p);
                }
                return bundle;
            }

            @Override
            public long getTimeToLive(String baseName, Locale locale)
            {
                return 1000 * 60 * 30;
            }

            @Override
            public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime)
            {
                return true;
            }

        };
    }
Run Code Online (Sandbox Code Playgroud)


dan*_*anb 2

您只是问如何在数据库中存储 UTF-8/16 字符吗?在 mysql 中,只需确保使用 UTF8 支持进行构建并将其设置为默认值,或者在列或表级别指定它即可。我以前在oracle和mysql中做过这个。创建一个表并将一些 i18n 数据剪切并粘贴到其中,看看会发生什么...您可能已经设置好了..

或者我完全没有理解你的意思?

编辑:

更明确地说...我通常实现一个三列表...语言,键,值...其中“值”包含潜在的外语单词或短语...“语言”包含一些语言键和“键”是英文密钥(即login.error.password.dup)...语言和密钥已索引...

然后,我在这样的结构上构建了界面,该结构显示每个键及其所有翻译(值)...它可以变得很奇特,包括审计跟踪和“脏”标记以及启用翻译器和数据所需的所有其他内容入门人士使用它..

编辑2:

现在您添加了有关 JSTL 标签的信息,我了解了更多...我自己从未这样做过..但我在服务器端找到了这个旧信息...

HttpSession session = .. [get hold of the session] 
ResourceBundle bundle = new PropertyResourceBundle(toInputStream(myOwnProperties)) [toInputStream just stores the properties into an inputstream] 
Locale locale = .. [get hold of the locale]
javax.servlet.jsp.jstl.core.Config.set(session, Config.FMT_LOCALIZATION_CONTEXT, new LocalizationContext(bundle ,locale));
Run Code Online (Sandbox Code Playgroud)