android - 在模型对象中引用 Strings.XML 以支持本地化

标签 android resources localization model

我正在找到这个问题的公认答案和讨论 getString Outside of a Context or Activity不清楚。

我是 Android 新手,我正在尝试了解如何在模型类中引用资源字符串,以便能够正确支持本地化。

具体来说,我的模型具有 Location 属性,我希望能够返回方位罗盘序数的字符串。因为像“North”这样的罗盘序数需要本地化,所以我尝试将它们存储在我的 strings.xml 中。

我想我明白我需要应用程序上下文来访问资源对象,但我想知道这是否可以在不必传递上下文的情况下实现。在模型中存储 UI 上下文似乎违反了 MVC。

为了实现这一目标,我想在我的模型中包含一个像这样的方法。第一个 if 显示我如何尝试使用 strings.xml 条目。

public String compassOrdinalForBearing(float bearing) {

assert bearing >= 0.0 && bearing <= 360.0;

if ((bearing > 336.5) && (bearing <= 360.0))
    //Problem here
    return Context.getResources().getString(R.string.compass_ordinal_north);
else if ((bearing >= 0) && (bearing <= 22.5))
    return "North";
else if ((bearing > 22.5)  && (bearing <= 67.5))
    return "Northeast";
else if ((bearing > 67.5)  && (bearing <= 112.5))
    return "East";
else if ((bearing > 112.5) && (bearing <= 157.5))
    return "Southeast"; 
else if ((bearing > 157.5) && (bearing <= 202.5))
    return "South";
else if ((bearing > 202.5) && (bearing <= 247.5))
    return "Southwest";
else if ((bearing > 247.5) && (bearing <= 292.5))
    return "West";
else if ((bearing > 292.5) && (bearing <= 337.5))
    return "Northwest";
else
    assert false;
    return null;
}

最佳答案

通常所做的是子类化 Application 类,保证只有一个实例。

应用程序子类:

public class MyApplication extends Application {
   private static Context mContext;

   @Override
   public void onCreate(){
      super.onCreate();
      mContext = this;
   }

   public static Context getContext(){
        return mContext;
   }
}

你的类(class):

public String compassOrdinalForBearing(float bearing) {
    Context context = MyApplication.getContext();

    String north = context.getResources().getString(R.string.compass_ordinal_north);)
}

但不要忘记更改 list :

<application android:name="com.example.myapp.MyApplication">

或者,您可以在实例化期间传入上下文,而不保留指向它的指针,因为几乎可以肯定这些对象将从上下文实例化。

private Static string NORTH = null;

public MyClass(Context context){
    initializeDirections(context);
}

private static void initializeDirections(Context context){
    if(NORTH == null){
        NORTH = context.getResources().getString(R.string.compass_ordinal_north);
    }
}

最后,两者的一种困惑组合,以防万一您确实无法在实例化时传递上下文,并且您不想将应用程序上下文保留在 Application 子类中:

public class MyApplication extends Application {
    @Override
    public void onCreate(){
        super.onCreate();
        MyClass.initializeDirections(this);
    }
}

public class MyClass{
    private static String NORTH = null;

    public static final void initializeDirections(Context context){
        NORTH = context.getResources().getString(R.string.compass_ordinal_north);
    }
}

编辑:在一个不相关的注释中,至少仅从这个唯一的 fragment 中,您可以杀死所有第一个条件。如果它已到达特定的“else”,则第一个条件必然为真。

例如:

else if ((bearing >= 0) && (bearing <= 22.5))
    return "North";
else if ((bearing > 22.5)  && (bearing <= 67.5))
    return "Northeast";

可能是:

else if (bearing <= 22.5)
    return "North";
else if (bearing <= 67.5)
    return "Northeast";

就好像bearing是!<= 22.5,它必然> 22.5。

这可能会或可能不会提高您的可读性,并且可能会或可能不会令您满意。只是一些可能不需要的两美分:)

关于android - 在模型对象中引用 Strings.XML 以支持本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7666215/

相关文章:

java - Android 媒体播放器在后台停止播放

java - 无法使用 Parse 运行应用程序

Android缓存图像的最佳方式

c# - 项目中资源文件夹和图像/文件的真实路径?

security - 如何识别https网站中的混合内容

android - 是否有相当于 "Android Localization Files Editor"的 android-studio?

android - 如何使android edittext提示中心和左光标

8086 资源的汇编语言

c# - 在 C# 中枚举 Windows 语言的最佳方法是什么?

Android:本地化标准文本,例如 OK、CANCEL 和 GPS 未激活 - 例如?