-
-
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
Open
gyanu2507
wants to merge
4
commits into
allknowledge34:main
Choose a base branch
from
gyanu2507:feature/multi-language-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
60c243b
Implement multi-language support with language selection screen
87743a4
Implement multi-language support with language selection screen
0e4d5c3
Fix multi-language support: correct typo, remove deprecated methods, …
d1f1651
Merge branch 'main' into feature/multi-language-support
gyanu2507 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/example/updateapp/Helpers/LocaleHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 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 | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/example/updateapp/UpdateAppApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 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)); | ||
| } | ||
| } | ||
|
|
74 changes: 74 additions & 0 deletions
74
app/src/main/java/com/example/updateapp/adapters/LanguageAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/example/updateapp/models/LanguageModel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| 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; | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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