excludedStrings;
105 | private Translatable translatable;
106 | private Language[] supportedLanguages;
107 | private Language[] autoTranslatedLanguages;
108 | private int[] excludedStringIds;
109 | private RestartStrategy restartStrategy;
110 |
111 | /**
112 | * Constructs {@link Builder}
113 | *
114 | * @param context
115 | * @param defaultLanguage application default language (the same as language used in values resource folder)
116 | */
117 | public Builder(Context context, Language defaultLanguage) {
118 | this.context = context;
119 | this.defaultLanguage = defaultLanguage;
120 | supportedStrings = new ArrayList<>();
121 | excludedStrings = new ArrayList<>();
122 | }
123 |
124 | /**
125 | * Provides languages that application supports natively.
126 | * Eg. Application with resources values and values-pl should declare
127 | * {@link Language#English} and {@link Language#Polish} as supported languages
128 | *
129 | * @param supportedLanguages
130 | */
131 | public Builder setSupportedLanguages(Language... supportedLanguages) {
132 | this.supportedLanguages = supportedLanguages;
133 | return this;
134 | }
135 |
136 | /**
137 | * Provides languages, which are generated by Linguist app.
138 | *
139 | * @param autoTranslatedLanguages
140 | */
141 | public Builder setAutoTranslatedLanguages(Language... autoTranslatedLanguages) {
142 | this.autoTranslatedLanguages = autoTranslatedLanguages;
143 | return this;
144 | }
145 |
146 | /**
147 | * Adds string classes as a source for resources.
148 | * eg. If your applciationId/packageName is com.my.super.app,
149 | * then the required resources class will be com.my.super.app.R.string.class
150 | *
151 | * @param clazz string resources class
152 | */
153 | public Builder addStrings(Class clazz) {
154 | supportedStrings.add(clazz);
155 | return this;
156 | }
157 |
158 | /**
159 | * Excludes from translation all strings within given string class eg.
160 | * .excludeStrings(com.google.android.gms.R.string.class)
161 | * .excludeStrings(android.support.design.R.string.class)
162 | * .excludeStrings(android.support.v7.appcompat.R.string.class)
163 | *
164 | * @param clazz excluded string classes
165 | */
166 | public Builder excludeStrings(Class clazz) {
167 | excludedStrings.add(clazz);
168 | return this;
169 | }
170 |
171 | /**
172 | * Linguist needs to restart the application, when new Language is set.
173 | * You can change default behavior by injecting your restarting strategy
174 | *
175 | * @param restartStrategy strategy of app restart after configuration change
176 | */
177 | public Builder setRestartStrategy(RestartStrategy restartStrategy) {
178 | this.restartStrategy = restartStrategy;
179 | return this;
180 | }
181 |
182 | /**
183 | * Linguist excludes resources be comparing default language with one of supported languages,
184 | * however if there is only default language supported, then you should exclude strings that
185 | * should not be automatically translated, like client id's or urls
186 | *
187 | *
188 | * eg.
189 | * R.string.default_web_client_id, R.string.firebase_database_url,
190 | * R.string.gcm_defaultSenderId, R.string.google_api_key, R.string.google_app_id,
191 | * R.string.google_crash_reporting_api_key, R.string.google_storage_bucket, R.string.project_id
192 | *
193 | * @param stringId excluded string resources
194 | */
195 | public Builder excludeString(int... stringId) {
196 | excludedStringIds = stringId;
197 | return this;
198 | }
199 |
200 | /**
201 | * Builds {@link Options}
202 | */
203 | public Options build() {
204 | Options options = new Options(context, defaultLanguage);
205 | if (excludedStringIds == null) {
206 | excludedStringIds = new int[0];
207 | }
208 | ArrayList ids = new ArrayList<>();
209 | for (int stringId : excludedStringIds) {
210 | ids.add(stringId);
211 | }
212 |
213 | options.setExcludedStringIds(ids);
214 | options.setStringClasses(supportedStrings);
215 | if (supportedLanguages == null) {
216 | supportedLanguages = new Language[]{defaultLanguage};
217 | }
218 | excludedStrings.add(mobi.klimaszewski.linguist.R.string.class);
219 | options.setExcludedClasses(excludedStrings);
220 | ArrayList languages = new ArrayList<>();
221 | boolean isDefaultLanguageAdded = false;
222 | for (Language supportedLanguage : supportedLanguages) {
223 | if (supportedLanguage == defaultLanguage) {
224 | isDefaultLanguageAdded = true;
225 | }
226 | languages.add(supportedLanguage);
227 | }
228 | if (!isDefaultLanguageAdded) {
229 | languages.add(defaultLanguage);
230 | }
231 | options.setSupportedLanguages(languages);
232 | List autoTranslatedLanguages;
233 | if (this.autoTranslatedLanguages == null) {
234 | autoTranslatedLanguages = Collections.emptyList();
235 | } else {
236 | autoTranslatedLanguages = new ArrayList<>(Arrays.asList(this.autoTranslatedLanguages));
237 | autoTranslatedLanguages.removeAll(languages);
238 | }
239 | options.setAutoTranslatedLanguages(autoTranslatedLanguages);
240 | if (restartStrategy == null) {
241 | restartStrategy = new DefaultRestartStrategy(context);
242 | }
243 | options.setRestartStrategy(restartStrategy);
244 |
245 | return options;
246 | }
247 | }
248 |
249 | private static class DefaultRestartStrategy implements RestartStrategy {
250 |
251 | private final Context context;
252 |
253 | public DefaultRestartStrategy(Context context) {
254 | this.context = context;
255 | }
256 |
257 | @Override
258 | public void restart() {
259 | new Handler().postDelayed(new Runnable() {
260 | @Override
261 | public void run() {
262 | triggerRebirth(context);
263 | }
264 | }, 100);
265 | }
266 |
267 | public static void triggerRebirth(Context context) {
268 | PackageManager packageManager = context.getPackageManager();
269 | Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
270 | intent.addFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); // In case we are called with non-Activity context.
271 | context.startActivity(intent);
272 | if (context instanceof Activity) {
273 | ((Activity) context).finish();
274 | }
275 | Runtime.getRuntime().exit(0);
276 | }
277 | }
278 | }
279 |
--------------------------------------------------------------------------------