Skip to content

Commit

Permalink
Changed LocaleManager to rely on PerAppLocaleManager for the current …
Browse files Browse the repository at this point in the history
…language code
  • Loading branch information
nbradbury committed Dec 17, 2024
1 parent 0ec991d commit 715af69
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ private void getNoteFromNoteId(String noteId, RestRequest.Listener listener,
}

HashMap<String, String> params = new HashMap<>();
params.put("locale", LocaleManager.getLanguage(mContext));
params.put("locale", LocaleManager.getLanguage());
WordPress.getRestClientUtils().getNotification(params, noteId, listener, errorListener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean onStopJob(JobParameters params) {
public void onCreate() {
super.onCreate();
AppLog.i(AppLog.T.NOTIFS, "notifications update job service > created");
mNotificationsUpdateLogic = new NotificationsUpdateLogic(LocaleManager.getLanguage(this), this);
mNotificationsUpdateLogic = new NotificationsUpdateLogic(LocaleManager.getLanguage(), this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public IBinder onBind(Intent intent) {
public void onCreate() {
super.onCreate();
AppLog.i(AppLog.T.NOTIFS, "notifications update service > created");
mNotificationsUpdateLogic = new NotificationsUpdateLogic(LocaleManager.getLanguage(this), this);
mNotificationsUpdateLogic = new NotificationsUpdateLogic(LocaleManager.getLanguage(), this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2571,7 +2571,7 @@ class EditPostActivity : AppCompatActivity(), EditorFragmentActivity, EditorImag
get() {
val postType = if (isPage) "page" else "post"
val featuredImageId = editPostRepository.featuredImageId.toInt()
val languageString = LocaleManager.getLanguage(this@EditPostActivity)
val languageString = LocaleManager.getLanguage()
val wpcomLocaleSlug = languageString.replace("_", "-").lowercase()

// this.mIsXPostsCapable may return true for non-WP.com sites, but the app only supports xPosts for P2-based
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
mLanguagePreference = (WPPreference) findPreference(getString(R.string.pref_key_language));
mLanguagePreference.setOnPreferenceChangeListener(this);
mLanguagePreference.setOnPreferenceClickListener(this);
mLanguagePreference.setSummary(mPerAppLocaleManager.getCurrentLocaleDisplayName());
mLanguagePreference.setSummary(PerAppLocaleManager.Companion.getCurrentLocaleDisplayName());

return view;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ReaderInterestsFragment : Fragment(R.layout.reader_interests_fragment_layo
}

viewModel.start(
LocaleManager.getLanguage(WordPress.getContext()),
LocaleManager.getLanguage(),
parentViewModel,
entryPoint
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public enum UpdateTask {
public ReaderUpdateLogic(Context context, WordPress app, ServiceCompletionListener listener) {
mCompletionListener = listener;
app.component().inject(this);
mLanguage = LocaleManager.getLanguage(app);
mLanguage = LocaleManager.getLanguage();
mContext = context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Configuration
import android.text.TextUtils
import androidx.preference.PreferenceManager
import org.wordpress.android.R
import org.wordpress.android.util.PerAppLocaleManager.Companion.OLD_LOCALE_PREF_KEY_STRING
import java.text.Collator
import java.util.Locale
import java.util.regex.Pattern
Expand All @@ -29,7 +27,7 @@ object LocaleManager {
*/
@JvmStatic
fun setLocale(context: Context): Context {
return updateResources(context, getLanguage(context))
return updateResources(context, getLanguage())
}

/**
Expand All @@ -43,16 +41,16 @@ object LocaleManager {
return Locale.getDefault().toString() == newLocale.toString()
}

@Suppress("ForbiddenComment")
/**
* If the user has selected a language other than the device default, return that
* language code, else just return the device default language code.
* This is simply a wrapper for the per-app language code.
* TODO: Remove this and directly call PerAppLocaleManager
*
* @return The 2-letter language code (example "en")
*/
@JvmStatic
fun getLanguage(context: Context): String {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getString(OLD_LOCALE_PREF_KEY_STRING, LanguageUtils.getCurrentDeviceLanguageCode())!!
fun getLanguage(): String {
return PerAppLocaleManager.getCurrentLocaleLanguageCode()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LocaleManagerWrapper
fun getLocale(): Locale = Locale.getDefault()
fun getTimeZone(): TimeZone = TimeZone.getDefault()
fun getCurrentCalendar(): Calendar = Calendar.getInstance(getLocale())
fun getLanguage(): String = LocaleManager.getLanguage(context)
fun getLanguage(): String = LocaleManager.getLanguage()
fun isSameLanguage(language: String): Boolean = LocaleManager.isSameLanguage(language)
fun setLocale(context: Context): Context = LocaleManager.setLocale(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class PerAppLocaleManager @Inject constructor(
ReaderUpdateServiceStarter.startService(getContext(), EnumSet.of(UpdateTask.TAGS))
}


companion object {
// Key previously used for saving the language selection to shared preferences.
const val OLD_LOCALE_PREF_KEY_STRING: String = "language-pref"
Expand Down

0 comments on commit 715af69

Please sign in to comment.