Android应用Java/JSON转字符串及多语言翻译技术求助
Hey Nico, let's break down your problem into two clear parts and solve them with Android best practices:
Right now, you're hardcoding labels like "Shipping Date:" directly in your Java code, which makes translation a nightmare. Instead, we'll move all static, translatable text into Android's resource files — the standard way to handle localization for Android apps.
Step 1: Add Translatable Strings to strings.xml
Create or update the res/values/strings.xml file with your formatted labels. You can include HTML tags directly in the string resources:
<resources> <string name="shipping_date_label"><b>Shipping Date:</b></string> <string name="time_label"><b>Time:</b></string> <string name="shipping_address_label"><b>Shipping Address:</b></string> <string name="description_label"><b>Description:</b></string> <string name="weight_label"><b>Weight:</b></string> <string name="weight_suffix"> KG</string> <string name="destination_date_label"><b>Destination Date:</b></string> <string name="destination_address_label"><b>Destination Address:</b></string> </resources>
For other languages, create a new resource directory like res/values-es (for Spanish) and add a translated strings.xml there. Android will automatically pick the right file based on the device's language settings.
Step 2: Update Your Java Code
Instead of concatenating hardcoded strings, pull the localized labels from resources and combine them with your dynamic values. Use String.format() for cleaner, more maintainable code:
// Shipping Date String shippingDateText = String.format(getString(R.string.shipping_date_label) + "%s", shipping_date.toString()); loaddate.setText(Html.fromHtml(shippingDateText)); // Time String timeText = String.format(getString(R.string.time_label) + "%s", shipping_time.toString()); loadtime.setText(Html.fromHtml(timeText)); // Shipping Address String shippingAddressText = String.format(getString(R.string.shipping_address_label) + "%s", shipping_address_address.toString()); loadplace.setText(Html.fromHtml(shippingAddressText)); // Description String descText = String.format(getString(R.string.description_label) + "%s", description.toString()); txtdesc.setText(Html.fromHtml(descText)); // Weight (combined label + value + suffix) String weightText = String.format("%s%s%s", getString(R.string.weight_label), weight.toString(), getString(R.string.weight_suffix)); txtweight.setText(Html.fromHtml(weightText)); // Destination Date String destDateText = String.format(getString(R.string.destination_date_label) + "%s", destination_date.toString()); txtdestdate.setText(Html.fromHtml(destDateText)); // Destination Time String destTimeText = String.format(getString(R.string.time_label) + "%s", destination_time.toString()); txtdesttime.setText(Html.fromHtml(destTimeText)); // Destination Address String destAddressText = String.format(getString(R.string.destination_address_label) + "%s", destination_address_address.toString()); txtdestplace.setText(Html.fromHtml(destAddressText));
Note: I fixed the typo in your third string u (it had c.getString(shipping_address) which didn't make sense) to use the proper resource string instead.
If you need to work with Java objects or JSON data for translation purposes, here are two common scenarios:
Scenario 1: Java Objects → Translatable Strings
If you have a Java object (e.g., ShipmentDetails) with static labels and dynamic values, separate the labels into strings.xml and use the object's values to build localized text. For example:
First, define your object:
public class ShipmentDetails { private Date shippingDate; private String shippingTime; // ... other fields // Getters public Date getShippingDate() { return shippingDate; } public String getShippingTime() { return shippingTime; } }
Then, use it to generate localized text:
ShipmentDetails details = getShipmentDetails(); // Your object instance String shippingDateText = String.format(getString(R.string.shipping_date_label) + "%s", details.getShippingDate().toString()); loaddate.setText(Html.fromHtml(shippingDateText));
If you need to serialize the object to a structured string (for logging or sending to a translation service), use Gson to convert it to JSON:
import com.google.gson.Gson; // Convert object to JSON string Gson gson = new Gson(); String jsonString = gson.toJson(details);
Scenario 2: JSON → Translatable Strings
If you're receiving JSON data with hardcoded labels, don't use those labels directly. Map the JSON keys to your strings.xml resources instead. For example, if your JSON looks like this:
{ "shipping_date_label": "Shipping Date:", "shipping_date": "2024-05-20" }
Parse the JSON and replace the label with your localized resource:
import org.json.JSONObject; JSONObject shipmentJson = new JSONObject(jsonResponse); String shippingDateValue = shipmentJson.getString("shipping_date"); // Use the localized label instead of the JSON's hardcoded label String shippingDateText = String.format(getString(R.string.shipping_date_label) + "%s", shippingDateValue); loaddate.setText(Html.fromHtml(shippingDateText));
This ensures your app uses the correct localized text regardless of the JSON's content.
内容的提问来源于stack exchange,提问作者Nicola Pigozzo

