Matisse icon indicating copy to clipboard operation
Matisse copied to clipboard

【建议改进】避免 getQuantityString 的异常处理

Open yccheok opened this issue 4 years ago • 2 comments

现有的代码如下

https://github.com/zhihu/Matisse/blob/master/matisse/src/main/java/com/zhihu/matisse/internal/model/SelectedItemCollection.java#L172

        String cause;

        try {
            cause = mContext.getResources().getQuantityString(
                    R.plurals.error_over_count,
                    maxSelectable,
                    maxSelectable
            );
        } catch (Resources.NotFoundException e) {
            cause = mContext.getString(
                    R.string.error_over_count,
                    maxSelectable
            );
        } catch (NoClassDefFoundError e) {
            cause = mContext.getString(
                    R.string.error_over_count,
                    maxSelectable
            );
        }

我建议把她改为

        String cause = mContext.getResources().getQuantityString(
                R.plurals.error_over_count,
                maxSelectable,
                maxSelectable
        );

然后,在所有的 strings.xml

从 (加泰罗尼亚语)

<string name="error_over_count">Només pots seleccionar fins a %1$d arxius multimedia</string>

改为 (加泰罗尼亚语)

<plurals name="error_over_count">
    <item quantity="one">Només pots seleccionar fins a %1$d arxius multimedia</item>
    <item quantity="other">Només pots seleccionar fins a %1$d arxius multimedia</item>
</plurals>

或 (波兰语,IDE 会针对不同语言,给出正确的格式。不同语言,会有不同格式)

<plurals name="error_over_count">
    <item quantity="few">Możesz wybrać do %1$d plików</item>
    <item quantity="many">Możesz wybrać do %1$d plików</item>
    <item quantity="one">Możesz wybrać do %1$d plików</item>
    <item quantity="other">Możesz wybrać do %1$d plików</item>
</plurals>

还需要各自翻译员再次正确翻译。不过,这会比现有的方案好很多。因为

1)提供正确统一平台给翻译员翻译 2)避免异常处理

我之前有个老旧旁枝做了个示范。已经在个有百万级用户的应用运行超过两年。应该正确无误。

https://github.com/yccheok/Matisse/commit/926861e0a62ea62663af8d006fef9e29c408e6f4

如有兴趣,我很乐意提交拉取请求。

谢谢

yccheok avatar May 23 '20 14:05 yccheok

同时,我发现 error_over_count_default 并没被代码采用。是否应该删除?

yccheok avatar May 23 '20 14:05 yccheok

gbkus123 avatar Mar 06 '23 09:03 gbkus123