aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/utils/TranslationUtils.java
blob: 4d318f087a7fcffcb7324cfe00ec846b9f253bf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cgeo.geocaching.utils;

import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.network.Network;

import android.content.Intent;
import android.net.Uri;

/**
 * Utilities used for translating
 */
public final class TranslationUtils {

    private static final String translationWebsite = "http://translate.google.com/";
    private static final String translationForceClassicMode = "?vi=c";
    private static final String translationAutoSelect = "#auto";
    private static final String translationFieldSeparator = "|";

    public static final int translationTextLengthToWarn = 500;
    private static final String TRANSLATION_APP = "com.google.android.apps.translate";

    /**
     * Build a URI for Google Translate
     *
     * @param toLang
     *            The two-letter lowercase ISO language codes as defined by ISO 639-1
     * @param text
     *            The text to be translated
     * @return URI ready to be parsed
     */
    private static String buildTranslationURI(final String toLang, final String text) {
        String content = Network.encode(text);
        // the app works better without the "+", the website works better with "+", therefore assume using the app if installed
        if (ProcessUtils.isInstalled(TRANSLATION_APP)) {
            content = content.replace("+", "%20");
        }
        return translationWebsite + translationForceClassicMode + translationAutoSelect + translationFieldSeparator + toLang + translationFieldSeparator + content;
    }

    /**
     * Send Intent for Google Translate. Can be caught by Google Translate App or browser
     *
     * @param toLang
     *            The two-letter lowercase ISO language codes as defined by ISO 639-1
     * @param text
     *            The text to be translated
     */
    public static void startActivityTranslate(final AbstractActivity context, final String toLang, final String text) {
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(buildTranslationURI(toLang, text))));
    }
}