InAppTranslation automatically translates Android Widgets' texts at runtime by the power of Data Binding + Google Translate.
Just by replacing android:text tag with app:localizeText, your app starts showing the UI in the user's local language.
compile 'com.goldrushcomputing.inapptranslation:inapptranslation:0.9.3'InAppTranslation uses Google Translate. Get the api key from https://cloud.google.com/translate/
Set the key in local.properties file as below.
inapptranslation.google_translate_apikey="Your_Google_Translate_API_Key"(Don't share the local.properties file on GitHub.)
(If your project is not ready for Data Binding, please do so by following
Data Binding Library - Android Developers )
It is very simple to use InAppTranslation.
Instead of using android:text="Hello", use the tag below.
app:localizeText="@{`Hello`}"Here is an example of TextView using the tag.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:localizeText="@{`Hello`}"/>You can also specify string resource.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:localizeText="@{@string/start}"/>app:localizeText tag supports
- TextView
- Button
- CheckBox
- RadioButton
- CheckedTextView
- EditText
For ToggleButton and Switch, use
app:localizeTextOff="@{`Off`}"
app:localizeTextOn="@{`On`}"which works like android:textOff and android:textOn.
For EditText's hint text, you can use below hint tag
app:localizeHint="@{`Type your name`}"Changing textOff and textOn property of Switch from Java code doesn't change the actual texts on the UI. It seems like a bug in Android.
Due to this limitation, dynamically translated texts for Switch textOn/Off properties are not displayed. To workaround this, I've added a class called IATSwitch.
To use this, first import IATSwitch by adding the code below in <data></data> section in your layout.xml file.
<import type="com.goldrushcomputing.inapptranslation.IATSwitch" />Then, replace Switch with IATSwtich as below
<com.goldrushcomputing.inapptranslation.IATSwitch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:showText="true"
app:localizeTextOff="@{`Off`}"
app:localizeTextOn="@{`Active`}"/>InAppTranslation library uses Google Translate api to translate texts.
The source language is automatically inferred by Google Translate.
InAppTranslation sets the target language to the user's phone language.
In case you don't use English as your base language, there is a chance that Google Translate doesn't recognize the source language properly.
To avoid that, you can specify the base language by yourself.
Add below code in your custom Application class's onCreate method.
(You may want to change "ja" part of the code below if your base language is not Japanese.)
InAppTranslation.setSourceLanguage("ja"); //translate from Japanese- Provide a method to simply get translation data in Java code, in order to support localizing dynamic content such as news
- Cache translation results for better performance
- Better collaborate with strings.xml - use strings.xml if translation already exists.
Would love to have proguard pull request for consumer proguard implementation
MIT License Copyright (c) 2017 Takamitsu Mizutori Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.