Skip to content

Commit 73f0497

Browse files
authored
Merge pull request #9249 from wordpress-mobile/update/send-gutenberg-translations
Send all translations to gutenberg-mobile
2 parents 7b41272 + 15a9701 commit 73f0497

File tree

4 files changed

+100
-18
lines changed

4 files changed

+100
-18
lines changed

WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,6 @@ protected void onCreate(Bundle savedInstanceState) {
410410

411411
// Check whether to show the visual editor
412412
PreferenceManager.setDefaultValues(this, R.xml.account_settings, false);
413-
// AppPrefs.setAztecEditorAvailable(true);
414-
// AppPrefs.setAztecEditorEnabled(true);
415413
mShowAztecEditor = AppPrefs.isAztecEditorEnabled();
416414
mShowNewEditor = AppPrefs.isVisualEditorEnabled();
417415

@@ -450,8 +448,7 @@ protected void onCreate(Bundle savedInstanceState) {
450448
}
451449
newPostSetup();
452450
} else if (extras != null) {
453-
// Load post passed in extras
454-
mPost = mPostStore.getPostByLocalPostId(extras.getInt(EXTRA_POST_LOCAL_ID));
451+
mPost = mPostStore.getPostByLocalPostId(extras.getInt(EXTRA_POST_LOCAL_ID)); // Load post from extras
455452

456453
if (mPost != null) {
457454
initializePostObject();
@@ -476,8 +473,7 @@ protected void onCreate(Bundle savedInstanceState) {
476473

477474
showDialogProgress(mIsDialogProgressShown);
478475

479-
// if we have a remote id saved, let's first try with that, as the local Id might have changed
480-
// after FETCH_POSTS
476+
// if we have a remote id saved, let's first try that, as the local Id might have changed after FETCH_POSTS
481477
if (savedInstanceState.containsKey(STATE_KEY_POST_REMOTE_ID)) {
482478
mPost = mPostStore.getPostByRemotePostId(savedInstanceState.getLong(STATE_KEY_POST_REMOTE_ID), mSite);
483479
initializePostObject();
@@ -544,9 +540,8 @@ protected void onCreate(Bundle savedInstanceState) {
544540
if (mIsNewPost) {
545541
trackEditorCreatedPost(action, getIntent());
546542
} else {
547-
// if we are opening a Post for which an error notification exists, we need to remove
548-
// it from the dashboard to prevent the user from tapping RETRY on a Post that is
549-
// being currently edited
543+
// if we are opening a Post for which an error notification exists, we need to remove it from the dashboard
544+
// to prevent the user from tapping RETRY on a Post that is being currently edited
550545
UploadService.cancelFinalNotification(this, mPost);
551546
resetUploadingMediaToFailedIfPostHasNotMediaInProgressOrQueued();
552547
}
@@ -560,9 +555,8 @@ protected void onCreate(Bundle savedInstanceState) {
560555
mViewPager.setOffscreenPageLimit(3);
561556
mViewPager.setPagingEnabled(false);
562557

563-
// When swiping between different sections, select the corresponding
564-
// tab. We can also use ActionBar.Tab#select() to do this if we have
565-
// a reference to the Tab.
558+
// When swiping between different sections, select the corresponding tab. We can also use ActionBar.Tab#select()
559+
// to do this if we have a reference to the Tab.
566560
mViewPager.clearOnPageChangeListeners();
567561
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
568562
@Override
@@ -2211,7 +2205,9 @@ public Fragment getItem(int position) {
22112205
if (mShowGutenbergEditor) {
22122206
// Show the GB informative dialog on editing GB posts
22132207
showGutenbergInformativeDialog();
2214-
return GutenbergEditorFragment.newInstance("", "", mIsNewPost);
2208+
String languageString = LocaleManager.getLanguage(EditPostActivity.this);
2209+
String wpcomLocaleSlug = languageString.replace("_", "-").toLowerCase(Locale.ENGLISH);
2210+
return GutenbergEditorFragment.newInstance("", "", mIsNewPost, wpcomLocaleSlug);
22152211
} else if (mShowAztecEditor) {
22162212
return AztecEditorFragment.newInstance("", "",
22172213
AppPrefs.isAztecEditorToolbarExpanded());

libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergContainerFragment.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ public class GutenbergContainerFragment extends Fragment {
1414
public static final String TAG = "gutenberg_container_fragment_tag";
1515

1616
private static final String ARG_IS_NEW_POST = "param_is_new_post";
17+
private static final String ARG_LOCALE = "param_locale";
18+
private static final String ARG_TRANSLATIONS = "param_translations";
1719

1820
private boolean mHtmlModeEnabled;
1921
private boolean mHasReceivedAnyContent;
2022

2123
private WPAndroidGlueCode mWPAndroidGlueCode;
2224

23-
public static GutenbergContainerFragment newInstance(boolean isNewPost) {
25+
public static GutenbergContainerFragment newInstance(boolean isNewPost, String localeString, Bundle translations) {
2426
GutenbergContainerFragment fragment = new GutenbergContainerFragment();
2527
Bundle args = new Bundle();
2628
args.putBoolean(ARG_IS_NEW_POST, isNewPost);
29+
args.putString(ARG_LOCALE, localeString);
30+
args.putBundle(ARG_TRANSLATIONS, translations);
2731
fragment.setArguments(args);
2832
return fragment;
2933
}
@@ -44,6 +48,8 @@ public void onCreate(Bundle savedInstanceState) {
4448
super.onCreate(savedInstanceState);
4549

4650
boolean isNewPost = getArguments() != null && getArguments().getBoolean(ARG_IS_NEW_POST);
51+
String localeString = getArguments().getString(ARG_LOCALE);
52+
Bundle translations = getArguments().getBundle(ARG_TRANSLATIONS);
4753

4854
mWPAndroidGlueCode = new WPAndroidGlueCode();
4955
mWPAndroidGlueCode.onCreate(getContext());
@@ -53,7 +59,9 @@ public void onCreate(Bundle savedInstanceState) {
5359
getActivity().getApplication(),
5460
BuildConfig.DEBUG,
5561
BuildConfig.BUILD_GUTENBERG_FROM_SOURCE,
56-
isNewPost);
62+
isNewPost,
63+
localeString,
64+
translations);
5765

5866
// clear the content initialization flag since a new ReactRootView has been created;
5967
mHasReceivedAnyContent = false;

libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.DialogInterface;
77
import android.content.DialogInterface.OnClickListener;
88
import android.content.res.Configuration;
9+
import android.content.res.Resources;
910
import android.os.Bundle;
1011
import android.os.Handler;
1112
import android.support.annotation.NonNull;
@@ -16,6 +17,7 @@
1617
import android.support.v7.app.AppCompatActivity;
1718
import android.text.Editable;
1819
import android.text.Spanned;
20+
import android.util.DisplayMetrics;
1921
import android.view.ContextThemeWrapper;
2022
import android.view.LayoutInflater;
2123
import android.view.Menu;
@@ -41,6 +43,10 @@
4143
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnMediaLibraryButtonListener;
4244
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnReattachQueryListener;
4345

46+
import java.lang.reflect.Field;
47+
import java.util.ArrayList;
48+
import java.util.Arrays;
49+
import java.util.Locale;
4450
import java.util.HashSet;
4551
import java.util.Set;
4652
import java.util.concurrent.ConcurrentHashMap;
@@ -50,6 +56,7 @@ public class GutenbergEditorFragment extends EditorFragmentAbstract implements
5056
IHistoryListener {
5157
private static final String KEY_HTML_MODE_ENABLED = "KEY_HTML_MODE_ENABLED";
5258
private static final String ARG_IS_NEW_POST = "param_is_new_post";
59+
private static final String ARG_LOCALE_SLUG = "param_locale_slug";
5360

5461
private static final int CAPTURE_PHOTO_PERMISSION_REQUEST_CODE = 101;
5562

@@ -72,12 +79,14 @@ public class GutenbergEditorFragment extends EditorFragmentAbstract implements
7279

7380
public static GutenbergEditorFragment newInstance(String title,
7481
String content,
75-
boolean isNewPost) {
82+
boolean isNewPost,
83+
String localeSlug) {
7684
GutenbergEditorFragment fragment = new GutenbergEditorFragment();
7785
Bundle args = new Bundle();
7886
args.putString(ARG_PARAM_TITLE, title);
7987
args.putString(ARG_PARAM_CONTENT, content);
8088
args.putBoolean(ARG_IS_NEW_POST, isNewPost);
89+
args.putString(ARG_LOCALE_SLUG, localeSlug);
8190
fragment.setArguments(args);
8291
return fragment;
8392
}
@@ -94,17 +103,86 @@ private GutenbergContainerFragment getGutenbergContainerFragment() {
94103
return mRetainedGutenbergContainerFragment;
95104
}
96105

106+
/**
107+
* Returns the gutenberg-mobile specific translations
108+
*
109+
* @return Bundle a map of "english string" => [ "current locale string" ]
110+
*/
111+
public Bundle getTranslations() {
112+
Bundle translations = new Bundle();
113+
Locale defaultLocale = new Locale("en");
114+
Resources currentResources = getActivity().getApplicationContext().getResources();
115+
Configuration currentConfiguration = currentResources.getConfiguration();
116+
// if the current locale of the app is english stop here and return an empty map
117+
if (currentConfiguration.locale.equals(defaultLocale)) {
118+
return translations;
119+
}
120+
121+
// Let's create a Resources object for the default locale (english) to get the original values for our strings
122+
DisplayMetrics metrics = new DisplayMetrics();
123+
Configuration defaultLocaleConfiguration = new Configuration(currentConfiguration);
124+
defaultLocaleConfiguration.setLocale(defaultLocale);
125+
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
126+
Resources defaultResources = new Resources(getActivity().getAssets(), metrics, defaultLocaleConfiguration);
127+
128+
// Strings are only being translated in the WordPress package
129+
// thus we need to get a reference of the R class for this package
130+
// Here we assume the Application class is at the same level as the R class
131+
// It will not work if this lib is used outside of WordPress-Android,
132+
// in this case let's just return an empty map
133+
Class<?> rString;
134+
Package mainPackage = getActivity().getApplication().getClass().getPackage();
135+
136+
if (mainPackage == null) {
137+
return translations;
138+
}
139+
140+
try {
141+
rString = getActivity().getApplication().getClassLoader().loadClass(mainPackage.getName() + ".R$string");
142+
} catch (ClassNotFoundException ex) {
143+
return translations;
144+
}
145+
146+
for (Field stringField : rString.getDeclaredFields()) {
147+
int resourceId;
148+
try {
149+
resourceId = stringField.getInt(rString);
150+
} catch (IllegalArgumentException | IllegalAccessException iae) {
151+
AppLog.e(T.EDITOR, iae);
152+
continue;
153+
}
154+
155+
String fieldName = stringField.getName();
156+
// Filter out all strings that are not prefixed with `gutenberg_mobile_`
157+
if (!fieldName.startsWith("gutenberg_mobile_")) {
158+
continue;
159+
}
160+
161+
// Add the mapping english => [ translated ] to the bundle if both string are not empty
162+
String currentResourceString = currentResources.getString(resourceId);
163+
String defaultResourceString = defaultResources.getString(resourceId);
164+
if (currentResourceString.length() > 0 && defaultResourceString.length() > 0) {
165+
translations.putStringArrayList(
166+
defaultResourceString,
167+
new ArrayList<>(Arrays.asList(currentResourceString))
168+
);
169+
}
170+
}
171+
return translations;
172+
}
173+
97174
@Override
98175
public void onCreate(Bundle savedInstanceState) {
99176
super.onCreate(savedInstanceState);
100177

101178
if (getGutenbergContainerFragment() == null) {
102179
boolean isNewPost = getArguments().getBoolean(ARG_IS_NEW_POST);
180+
String localeSlug = getArguments().getString(ARG_LOCALE_SLUG);
103181

104182
FragmentManager fragmentManager = getChildFragmentManager();
105183
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
106184
GutenbergContainerFragment gutenbergContainerFragment =
107-
GutenbergContainerFragment.newInstance(isNewPost);
185+
GutenbergContainerFragment.newInstance(isNewPost, localeSlug, this.getTranslations());
108186
gutenbergContainerFragment.setRetainInstance(true);
109187
fragmentTransaction.add(gutenbergContainerFragment, GutenbergContainerFragment.TAG);
110188
fragmentTransaction.commitNow();

libs/gutenberg-mobile

Submodule gutenberg-mobile updated 35 files

0 commit comments

Comments
 (0)