rtl.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Use of this source code is governed by a BSD-style license that can be
3e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// found in the LICENSE file.
4e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
5e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith#ifndef BASE_I18N_RTL_H_
6e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith#define BASE_I18N_RTL_H_
7e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
8e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith#include <string>
9e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
10e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith#include "base/compiler_specific.h"
11e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith#include "base/i18n/base_i18n_export.h"
12e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith#include "base/string16.h"
134874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko#include "build/build_config.h"
14e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
15e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithclass FilePath;
16e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
17e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithnamespace base {
18e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithnamespace i18n {
19e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
20e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithconst char16 kRightToLeftMark = 0x200F;
21e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithconst char16 kLeftToRightMark = 0x200E;
22e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithconst char16 kLeftToRightEmbeddingMark = 0x202A;
23e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithconst char16 kRightToLeftEmbeddingMark = 0x202B;
24e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithconst char16 kPopDirectionalFormatting = 0x202C;
25e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithconst char16 kLeftToRightOverride = 0x202D;
26e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithconst char16 kRightToLeftOverride = 0x202E;
27e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
28e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smithenum TextDirection {
29e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith  UNKNOWN_DIRECTION,
30e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith  RIGHT_TO_LEFT,
31e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith  LEFT_TO_RIGHT,
32e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith};
33e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
34e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Get the locale that the currently running process has been configured to use.
35e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// The return value is of the form language[-country] (e.g., en-US) where the
36a189d8976f1193b788508a1a29b2e9d0aca06acaAlexander Kornienko// language is the 2 or 3 letter code from ISO-639.
37e992ed1a065d857947b3969e6b779c41cc35c234Alexander KornienkoBASE_I18N_EXPORT std::string GetConfiguredLocale();
38e992ed1a065d857947b3969e6b779c41cc35c234Alexander Kornienko
39a189d8976f1193b788508a1a29b2e9d0aca06acaAlexander Kornienko// Canonicalize a string (eg. a POSIX locale string) to a Chrome locale name.
40e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT std::string GetCanonicalLocale(const char* locale);
41c6dcea93b499b504da22f9921fc198423ad0b13bAlexander Kornienko
424874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko// Sets the default locale of ICU.
434874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko// Once the application locale of Chrome in GetApplicationLocale is determined,
44c6dcea93b499b504da22f9921fc198423ad0b13bAlexander Kornienko// the default locale of ICU need to be changed to match the application locale
454874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko// so that ICU functions work correctly in a locale-dependent manner.
464874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko// This is handy in that we don't have to call GetApplicationLocale()
47c6dcea93b499b504da22f9921fc198423ad0b13bAlexander Kornienko// everytime we call locale-dependent ICU APIs as long as we make sure
484874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko// that this is called before any locale-dependent API is called.
49c6dcea93b499b504da22f9921fc198423ad0b13bAlexander KornienkoBASE_I18N_EXPORT void SetICUDefaultLocale(const std::string& locale_string);
50c6dcea93b499b504da22f9921fc198423ad0b13bAlexander Kornienko
51c6dcea93b499b504da22f9921fc198423ad0b13bAlexander Kornienko// Returns true if the application text direction is right-to-left.
52e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT bool IsRTL();
53e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
54e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Returns whether the text direction for the default ICU locale is RTL.  This
55e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// assumes that SetICUDefaultLocale has been called to set the default locale to
56e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// the UI locale of Chrome.
57e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// NOTE: Generally, you should call IsRTL() instead of this.
58e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT bool ICUIsRTL();
59e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
60e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Returns the text direction for |locale_name|.
61e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT TextDirection GetTextDirectionForLocale(
62e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith    const char* locale_name);
63e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
64e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Given the string in |text|, returns the directionality of the first
65e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// character with strong directionality in the string. If no character in the
66e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// text has strong directionality, LEFT_TO_RIGHT is returned. The Bidi
67e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// character types L, LRE, LRO, R, AL, RLE, and RLO are considered as strong
68e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// directionality characters. Please refer to http://unicode.org/reports/tr9/
69e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// for more information.
70e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT TextDirection GetFirstStrongCharacterDirection(
71e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith    const string16& text);
72e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
73e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Given the string in |text|, this function modifies the string in place with
74e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// the appropriate Unicode formatting marks that mark the string direction
75e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// (either left-to-right or right-to-left). The function checks both the current
76e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// locale and the contents of the string in order to determine the direction of
77e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// the returned string. The function returns true if the string in |text| was
78e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// properly adjusted.
79e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith//
80e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Certain LTR strings are not rendered correctly when the context is RTL. For
81e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// example, the string "Foo!" will appear as "!Foo" if it is rendered as is in
82e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// an RTL context. Calling this function will make sure the returned localized
83e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// string is always treated as a right-to-left string. This is done by
84e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// inserting certain Unicode formatting marks into the returned string.
85e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith//
86e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// ** Notes about the Windows version of this function:
87e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// TODO(idana) bug 6806: this function adjusts the string in question only
88e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// if the current locale is right-to-left. The function does not take care of
89e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// the opposite case (an RTL string displayed in an LTR context) since
90e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// adjusting the string involves inserting Unicode formatting characters that
91e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Windows does not handle well unless right-to-left language support is
92e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// installed. Since the English version of Windows doesn't have right-to-left
93e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// language support installed by default, inserting the direction Unicode mark
94e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// results in Windows displaying squares.
95e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT bool AdjustStringForLocaleDirection(string16* text);
96e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
97e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Undoes the actions of the above function (AdjustStringForLocaleDirection).
98e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT bool UnadjustStringForLocaleDirection(string16* text);
99e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
100e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Returns true if the string contains at least one character with strong right
101e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// to left directionality; that is, a character with either R or AL Unicode
102e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// BiDi character type.
103e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT bool StringContainsStrongRTLChars(const string16& text);
104e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
105e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Wraps a string with an LRE-PDF pair which essentialy marks the string as a
106e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Left-To-Right string. Doing this is useful in order to make sure LTR
107e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// strings are rendered properly in an RTL context.
108e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT void WrapStringWithLTRFormatting(string16* text);
109e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
110e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Wraps a string with an RLE-PDF pair which essentialy marks the string as a
111e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Right-To-Left string. Doing this is useful in order to make sure RTL
112e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// strings are rendered properly in an LTR context.
113e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT void WrapStringWithRTLFormatting(string16* text);
114e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
115e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Wraps file path to get it to display correctly in RTL UI. All filepaths
116e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// should be passed through this function before display in UI for RTL locales.
117e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT void WrapPathWithLTRFormatting(const FilePath& path,
118e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith                                                string16* rtl_safe_path);
119e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
120e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Return the string in |text| wrapped with LRE (Left-To-Right Embedding) and
121e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// PDF (Pop Directional Formatting) marks, if needed for UI display purposes.
122e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT string16 GetDisplayStringInLTRDirectionality(
123e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith    const string16& text) WARN_UNUSED_RESULT;
124e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith
125e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// Strip the beginning (U+202A..U+202B, U+202D..U+202E) and/or ending (U+202C)
126e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// explicit bidi control characters from |text|, if there are any. Otherwise,
127e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// return the text itself. Explicit bidi control characters display and have
128e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// semantic effect. They can be deleted so they might not always appear in a
129e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith// pair.
130e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard SmithBASE_I18N_EXPORT string16 StripWrappingBidiControlCharacters(
131e0d3b4cd2b66f1cef26cacbed5820ab7c22ad5b3Richard Smith    const string16& text) WARN_UNUSED_RESULT;
1324874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko
1334874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko}  // namespace i18n
1344874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko}  // namespace base
1354874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko
1364874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko#endif  // BASE_I18N_RTL_H_
1374874a8143dc3032205f97527ff619730db3d1f57Alexander Kornienko