-
-
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 all commits
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,47 @@ | ||
| package com.example.updateapp.Helpers; | ||
|
|
||
| import android.content.Context; | ||
| import android.content.SharedPreferences; | ||
| import android.content.res.Configuration; | ||
| 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); | ||
|
|
||
| // Save language preference | ||
| SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); | ||
| prefs.edit().putString(KEY_LANGUAGE, languageCode).apply(); | ||
|
|
||
| // Note: Configuration update is handled by attachBaseContext when activity is recreated | ||
| // This avoids using deprecated updateConfiguration method | ||
| } | ||
|
|
||
| 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,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)); | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,4 +44,4 @@ public boolean isSelected() { | |
| public void setSelected(boolean selected) { | ||
| isSelected = selected; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| import androidx.core.view.ViewCompat; | ||
| import androidx.core.view.WindowInsetsCompat; | ||
|
|
||
| import com.example.updateapp.Helpers.LocaleHelper; | ||
| import com.example.updateapp.R; | ||
| import com.example.updateapp.databinding.ActivityNewUpdateBinding; | ||
| import com.example.updateapp.utils.LocaleHelper; | ||
|
Comment on lines
+14
to
17
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. Duplicate Two classes named
This will fail to compile. Choose one implementation and remove the other import. Based on the 🐛 Proposed fix-import com.example.updateapp.Helpers.LocaleHelper;
import com.example.updateapp.R;
import com.example.updateapp.databinding.ActivityNewUpdateBinding;
import com.example.updateapp.utils.LocaleHelper;🤖 Prompt for AI Agents |
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |||||||||||||||
| import androidx.annotation.NonNull; | ||||||||||||||||
| import androidx.appcompat.app.AppCompatActivity; | ||||||||||||||||
|
|
||||||||||||||||
| import com.example.updateapp.Helpers.LocaleHelper; | ||||||||||||||||
| import com.example.updateapp.R; | ||||||||||||||||
| import com.example.updateapp.databinding.ActivitySignUpBinding; | ||||||||||||||||
| import com.example.updateapp.utils.LocaleHelper; | ||||||||||||||||
|
Comment on lines
+14
to
17
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. Duplicate Same issue—remove the unused 🐛 Proposed fix-import com.example.updateapp.Helpers.LocaleHelper;
import com.example.updateapp.R;
import com.example.updateapp.databinding.ActivitySignUpBinding;
import com.example.updateapp.utils.LocaleHelper;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |||||||||||||||
| import androidx.core.view.ViewCompat; | ||||||||||||||||
| import androidx.core.view.WindowInsetsCompat; | ||||||||||||||||
|
|
||||||||||||||||
| import com.example.updateapp.Helpers.LocaleHelper; | ||||||||||||||||
| import com.example.updateapp.Helpers.SaveState; | ||||||||||||||||
| import com.example.updateapp.R; | ||||||||||||||||
| import com.example.updateapp.utils.LocaleHelper; | ||||||||||||||||
|
Comment on lines
+14
to
17
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. Duplicate Same issue as other files—two 🐛 Proposed fix-import com.example.updateapp.Helpers.LocaleHelper;
import com.example.updateapp.Helpers.SaveState;
import com.example.updateapp.R;
import com.example.updateapp.utils.LocaleHelper;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -28,21 +30,36 @@ protected void onCreate(Bundle savedInstanceState) { | |||||||||||||||
| ); | ||||||||||||||||
| setContentView(R.layout.activity_splash_screen); | ||||||||||||||||
|
|
||||||||||||||||
| // Check if language has been selected before | ||||||||||||||||
| SaveState languageState = new SaveState(this, "LANGUAGE_SELECTED"); | ||||||||||||||||
| boolean languageSelected = languageState.getState() == 1; | ||||||||||||||||
|
|
||||||||||||||||
| new Timer().schedule(new TimerTask() { | ||||||||||||||||
| @Override | ||||||||||||||||
| public void run() { | ||||||||||||||||
| runOnUiThread(new Runnable() { | ||||||||||||||||
| @Override | ||||||||||||||||
| public void run() { | ||||||||||||||||
| Intent intent = new Intent(); | ||||||||||||||||
| intent.setClass(SplashScreenActivity.this, OnboardingActivity.class); | ||||||||||||||||
| Intent intent; | ||||||||||||||||
| if (!languageSelected) { | ||||||||||||||||
| // Show language selection screen on first launch | ||||||||||||||||
| intent = new Intent(SplashScreenActivity.this, LanguageActivity.class); | ||||||||||||||||
| intent.putExtra("isFirstLaunch", true); | ||||||||||||||||
| } else { | ||||||||||||||||
| // Go to onboarding | ||||||||||||||||
| intent = new Intent(SplashScreenActivity.this, OnboardingActivity.class); | ||||||||||||||||
| } | ||||||||||||||||
| startActivity(intent); | ||||||||||||||||
| finish(); | ||||||||||||||||
| } | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
| },500); | ||||||||||||||||
| }, 500); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
| protected void attachBaseContext(Context newBase) { | ||||||||||||||||
| super.attachBaseContext(LocaleHelper.attachBaseContext(newBase)); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| @Override | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,4 +40,4 @@ | |
| android:layout_marginEnd="-20dp" | ||
| android:background="#F0F0F0" /> | ||
|
|
||
| </RelativeLayout> | ||
| </RelativeLayout> | ||
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