-
-
Notifications
You must be signed in to change notification settings - Fork 7
Implement multi-language support with language selection screen #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
60c243b
87743a4
0e4d5c3
d1f1651
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package com.example.updateapp.Helpers; | ||
|
|
||
| import android.content.Context; | ||
| import android.content.SharedPreferences; | ||
| import android.content.res.Configuration; | ||
| import android.content.res.Resources; | ||
| import android.os.Build; | ||
|
|
||
| import java.util.Locale; | ||
|
|
||
| public class LocaleHelper { | ||
| private static final String PREFS_NAME = "app_prefs"; | ||
| private static final String KEY_LANGUAGE = "selected_language"; | ||
|
|
||
| public static void setLocale(Context context, String languageCode) { | ||
| Locale locale = new Locale(languageCode); | ||
| Locale.setDefault(locale); | ||
|
|
||
| Configuration configuration = context.getResources().getConfiguration(); | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
| configuration.setLocale(locale); | ||
| } else { | ||
| configuration.locale = locale; | ||
| } | ||
|
|
||
| context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics()); | ||
|
|
||
| // Save language preference | ||
| SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | ||
| prefs.edit().putString(KEY_LANGUAGE, languageCode).apply(); | ||
| } | ||
|
|
||
| public static Context attachBaseContext(Context context) { | ||
| String languageCode = getSavedLanguage(context); | ||
| Locale locale = new Locale(languageCode); | ||
| Locale.setDefault(locale); | ||
|
|
||
| Configuration configuration = context.getResources().getConfiguration(); | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
| configuration.setLocale(locale); | ||
| return context.createConfigurationContext(configuration); | ||
| } else { | ||
| configuration.locale = locale; | ||
| context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics()); | ||
| return context; | ||
| } | ||
| } | ||
|
|
||
| public static String getSavedLanguage(Context context) { | ||
| SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | ||
| return prefs.getString(KEY_LANGUAGE, "en"); // Default to English | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.example.updateapp; | ||
|
|
||
| import android.app.Application; | ||
| import android.content.Context; | ||
|
|
||
| import com.example.updateapp.Helpers.LocaleHelper; | ||
|
|
||
| public class UpdateAppApplication extends Application { | ||
| @Override | ||
| protected void attachBaseContext(Context base) { | ||
| super.attachBaseContext(LocaleHelper.attachBaseContext(base)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package com.example.updateapp.adapters; | ||
|
|
||
| import android.view.LayoutInflater; | ||
| import android.view.View; | ||
| import android.view.ViewGroup; | ||
| import android.widget.ImageView; | ||
| import android.widget.TextView; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.recyclerview.widget.RecyclerView; | ||
|
|
||
| import com.example.updateapp.R; | ||
| import com.example.updateapp.models.LanguageModel; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class LanguageAdapter extends RecyclerView.Adapter<LanguageAdapter.ViewHolder> { | ||
|
|
||
| private List<LanguageModel> languageList; | ||
| private OnLanguageClickListener listener; | ||
|
|
||
| public interface OnLanguageClickListener { | ||
| void onLanguageClick(LanguageModel language); | ||
| } | ||
|
|
||
| public LanguageAdapter(List<LanguageModel> languageList, OnLanguageClickListener listener) { | ||
| this.languageList = languageList; | ||
| this.listener = listener; | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
| View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_language, parent, false); | ||
| return new ViewHolder(view); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | ||
| LanguageModel language = languageList.get(position); | ||
| holder.languageName.setText(language.getLanguageName()); | ||
| holder.selectedIcon.setVisibility(language.isSelected() ? View.VISIBLE : View.GONE); | ||
|
|
||
| holder.itemView.setOnClickListener(v -> { | ||
| if (listener != null) { | ||
| listener.onLanguageClick(language); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public int getItemCount() { | ||
| return languageList.size(); | ||
| } | ||
|
|
||
| public void updateSelection(String selectedLanguageCode) { | ||
| for (LanguageModel language : languageList) { | ||
| language.setSelected(language.getLanguageCode().equals(selectedLanguageCode)); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| notifyDataSetChanged(); | ||
| } | ||
|
|
||
| public class ViewHolder extends RecyclerView.ViewHolder { | ||
| TextView languageName; | ||
| ImageView selectedIcon; | ||
|
|
||
| public ViewHolder(@NonNull View itemView) { | ||
| super(itemView); | ||
| languageName = itemView.findViewById(R.id.txt_language_name); | ||
| selectedIcon = itemView.findViewById(R.id.img_selected); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.example.updateapp.models; | ||
|
|
||
| public class LanguageModel { | ||
| private String languageCode; | ||
| private String languageName; | ||
| private boolean isSelected; | ||
|
|
||
| public LanguageModel(String languageCode, String languageName, boolean isSelected) { | ||
| this.languageCode = languageCode; | ||
| this.languageName = languageName; | ||
| this.isSelected = isSelected; | ||
| } | ||
|
|
||
| public String getLanguageCode() { | ||
| return languageCode; | ||
| } | ||
|
|
||
| public void setLanguageCode(String languageCode) { | ||
| this.languageCode = languageCode; | ||
| } | ||
|
|
||
| public String getLanguageName() { | ||
| return languageName; | ||
| } | ||
|
|
||
| public void setLanguageName(String languageName) { | ||
| this.languageName = languageName; | ||
| } | ||
|
|
||
| public boolean isSelected() { | ||
| return isSelected; | ||
| } | ||
|
|
||
| public void setSelected(boolean selected) { | ||
| isSelected = selected; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||||||||||||||||||||||||||||||||
| <?xml version="1.0" encoding="utf-8"?> | ||||||||||||||||||||||||||||||||||||
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||||||||||||||||||||||||||||||||||||
| android:layout_width="match_parent" | ||||||||||||||||||||||||||||||||||||
| android:layout_height="wrap_content" | ||||||||||||||||||||||||||||||||||||
| android:padding="16dp" | ||||||||||||||||||||||||||||||||||||
| android:background="?android:attr/selectableItemBackground"> | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| <TextView | ||||||||||||||||||||||||||||||||||||
| android:id="@+id/txt_language_name" | ||||||||||||||||||||||||||||||||||||
| android:layout_width="wrap_content" | ||||||||||||||||||||||||||||||||||||
| android:layout_height="wrap_content" | ||||||||||||||||||||||||||||||||||||
| android:layout_centerVertical="true" | ||||||||||||||||||||||||||||||||||||
| android:text="English" | ||||||||||||||||||||||||||||||||||||
| android:textSize="16sp" | ||||||||||||||||||||||||||||||||||||
| android:textColor="@color/black" /> | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| <ImageView | ||||||||||||||||||||||||||||||||||||
| android:id="@+id/img_selected" | ||||||||||||||||||||||||||||||||||||
| android:layout_width="24dp" | ||||||||||||||||||||||||||||||||||||
| android:layout_height="24dp" | ||||||||||||||||||||||||||||||||||||
| android:layout_alignParentEnd="true" | ||||||||||||||||||||||||||||||||||||
| android:layout_centerVertical="true" | ||||||||||||||||||||||||||||||||||||
| android:src="@drawable/ic_done_24" | ||||||||||||||||||||||||||||||||||||
| android:visibility="gone" /> | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add contentDescription for accessibility. The ImageView is missing a 🔎 Proposed fix <ImageView
android:id="@+id/img_selected"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_done_24"
+ android:contentDescription="@string/selected"
android:visibility="gone" />Note: You'll need to add a string resource 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| </RelativeLayout> | ||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <resources> | ||
| <string name="app_name">UpdateApp</string> | ||
| <string name="hello_blank_fragment">Hello blank fragment</string> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Translate or document untranslated strings. The 🤖 Prompt for AI Agents |
||
| <string name="default_web_client_id">610596201338-8jkqtv5msdu5q7mi0qs0gho2tu8a8esv.apps.googleusercontent.com</string> | ||
| <string name="language">اللغة</string> | ||
| <string name="language_english">الإنجليزية</string> | ||
| <string name="language_chinese">الصينية</string> | ||
| <string name="language_hindi">الهندية</string> | ||
| <string name="language_spanish">الإسبانية</string> | ||
| <string name="language_arabic">العربية</string> | ||
| <string name="language_french">الفرنسية</string> | ||
| <string name="language_bengali">البنغالية</string> | ||
| <string name="language_portuguese">البرتغالية</string> | ||
| <string name="language_russian">الروسية</string> | ||
| <string name="language_urdu">الأردية</string> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Incomplete localization - only language selection strings present. This file contains only language selection UI strings. Per the reviewer's feedback, this is a blocking issue for PR approval and payment. The requirement is explicit: all visible text must be moved to strings.xml with the same string keys across all languages. Missing localizations include but are not limited to:
Without complete localization, switching to Arabic will only affect the LanguageActivity screen—exactly the issue the reviewer reported: "locale change only affects LanguageActivity; other screens (login, signup, profile, buttons, messages) do not update." Required action: Add complete string resources for every user-facing text element in the app, ensuring the same string keys exist across all locale files (values/, values-ar/, values-zh/, etc.) with appropriate translations for each language. Would you like me to help generate a comprehensive list of string keys that should be present across all locale files, or search the codebase to identify hardcoded strings that need to be externalized? 🤖 Prompt for AI Agents |
||
| </resources> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <resources> | ||
| <string name="app_name">UpdateApp</string> | ||
| <string name="hello_blank_fragment">Hello blank fragment</string> | ||
| <string name="default_web_client_id">610596201338-8jkqtv5msdu5q7mi0qs0gho2tu8a8esv.apps.googleusercontent.com</string> | ||
| <string name="language">ভাষা</string> | ||
| <string name="language_english">ইংরেজি</string> | ||
| <string name="language_chinese">চীনা</string> | ||
| <string name="language_hindi">হিন্দি</string> | ||
| <string name="language_spanish">স্প্যানিশ</string> | ||
| <string name="language_arabic">আরবি</string> | ||
| <string name="language_french">ফরাসি</string> | ||
| <string name="language_bengali">বাংলা</string> | ||
| <string name="language_portuguese">পর্তুগিজ</string> | ||
| <string name="language_russian">রাশিয়ান</string> | ||
| <string name="language_urdu">উর্দু</string> | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| </resources> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using deprecated API: updateConfiguration should be avoided for API 25+.
Line 26 uses
updateConfiguration(), which has been deprecated since API 25. For API 24+, the recommended approach is to usecreateConfigurationContext()instead. While the current implementation works becauseLanguageActivitycallsrecreate()immediately after, it's better to follow Android best practices.The deprecation is mentioned in Android docs:
updateConfigurationcan lead to unpredictable behavior and configuration inconsistencies.🔎 Proposed fix
Consider refactoring
setLocaleto avoid the deprecated API for newer Android versions:public static void setLocale(Context context, String languageCode) { Locale locale = new Locale(languageCode); Locale.setDefault(locale); - Configuration configuration = context.getResources().getConfiguration(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - configuration.setLocale(locale); - } else { - configuration.locale = locale; - } - - context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics()); - // Save language preference SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); prefs.edit().putString(KEY_LANGUAGE, languageCode).apply(); }Then ensure activities call
recreate()or restart themselves after callingsetLocale()to pick up the new locale throughattachBaseContext(). Your current implementation inLanguageActivity.onLanguageSelected()already does this correctly with therecreate()call.🤖 Prompt for AI Agents