Java 改为;“myfile.xml”;从各个地方
对于我的android应用程序的每个本地化,我在不同的Java 改为;“myfile.xml”;从各个地方,java,android,xml,file,localization,Java,Android,Xml,File,Localization,对于我的android应用程序的每个本地化,我在不同的myfile.xml文件中包含了一些数据,在: 值/myfile.xml 值de/myfile.xml 值en/myfile.xml rGB/myfile.xml中的值 有没有一种简单的方法可以从所有这些本地化文件中检索一些数据?(与其读取所有文件,不如检查文件是否正确,并使用XML读取器对其进行解析,然后检索所有本地化的值。即使可能,我也没有进行测试) 编辑,示例: 值/myfile.xml <resources>
myfile.xml
文件中包含了一些数据,在:
- 值/myfile.xml
- 值de/myfile.xml
- 值en/myfile.xml
- rGB/myfile.xml中的值
<resources>
<string name="url">http://example.com/</string>
</resources>
<resources>
<string name="url">http://example.net/</string>
</resources>
<resources>
<string name="url">http://example.org/</string>
</resources>
http://example.com/
值de/myfile.xml
<resources>
<string name="url">http://example.com/</string>
</resources>
<resources>
<string name="url">http://example.net/</string>
</resources>
<resources>
<string name="url">http://example.org/</string>
</resources>
http://example.net/
值en/myfile.xml
<resources>
<string name="url">http://example.com/</string>
</resources>
<resources>
<string name="url">http://example.net/</string>
</resources>
<resources>
<string name="url">http://example.org/</string>
</resources>
http://example.org/
从这些文件中,我想检索所有url的列表,无论应用程序运行在哪个区域
第二次编辑:
我尝试过的不是好方法的示例:
List<String> urls;
public void init(Context c){
Locale lBase = Locale.getDefault();
// Iter all languages availables
for(Lang l : LanguageSpinner.langs){
// set the locale
Locale locale = new Locale(l.getlCode());
updateLocale(c, locale);
// Retrieve data and push to the list
String url = c.getString(R.string.url);
urls.add(url);
}
// re-set the default locale
updateLocale(c, lBase);
}
public static void updateLocale(Context context, Locale l){
Locale.setDefault(l);
Configuration config = new Configuration();
config.locale = l;
context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());
}
列出url;
公共void init(上下文c){
Locale lBase=Locale.getDefault();
//国际热核实验堆所有语言可用
for(Lang l:LanguageSpinner.langs){
//设置区域设置
语言环境=新语言环境(l.getlCode());
UpdateScale(c,语言环境);
//检索数据并推送到列表
stringurl=c.getString(R.String.url);
添加(url);
}
//重新设置默认区域设置
更新尺度(c,lBase);
}
公共静态void updateScale(上下文,区域设置l){
Locale.setDefault(l);
配置配置=新配置();
config.locale=l;
context.getResources().updateConfiguration(配置,context.getResources().getDisplayMetrics());
}
好吧,如果你想解析一个XML文件,它就不是这样工作的
但是这个方法适合我(我使用它在WebView片段中显示本地化的html文件,用于提供“用户指南”):
将所有文件放在/assets
文件夹中(最终放在其他文件夹下,如/assets/data
)。将每个文件命名为
myfile en.xml
,myfile-de.xml
String strLang = "fr"; // or "en", "de", "es", "it", ...
String strFile = String.format
(
"file:///android_asset/data/myfile-%s.xml", strLang
);
strFile将包含本地化文件的路径
您可以从当前区域设置中自动设置strLang,如下所示:
String strLang = Locale.getDefault().getISO3Language().substring(0, 2);
如果只想检索本地化字符串,请使用以下语法:
String myString = getResources().getString(R.string.url);
你可以在每个文件夹中使用
Strings.xml
,我在每个文件夹中都有一个文件,我想读取所有文件,而不关心应用程序运行在哪个区域。myfile.xml将根据位置自动读取,所以我需要提取我想要的所有数据并将其放在另一个共享xml文件中?没有办法完成我想要的吗?values
文件夹不是保存文件的好地方。尝试使用assets
文件夹。我实际上无法选择您的方法,文件已经存在,并且包含很多值,并且与string.xml“engine”一起使用,那又怎样?将文件从一个文件夹移动到另一个文件夹需要多少时间?我没有要求修改结构,只是“可以吗?”。我有18个本地化,我不能也不想改变文件结构,我只想得到每个本地化xml文件的1个值。(我知道如何使用“资产”或其他技术来做到这一点),对不起。那么,你不需要你要求的。您要求解析XML文件(并使用XML读取器解析它)。而您只需要使用String myString=getResources().getString(R.String.url)代码>语法。没有XML解析。那么您的回答是,对区域设置进行18次黑客攻击以检索包含所有这些值的列表是保持我的文件结构的唯一方法?