Ejemplo n.º 1
0
		public static string TextDirectionLanguageFile = null; //Set during testing

		/// <summary>
		/// Looks up the text direction for the specified language code in the appropriate .ldml file.
		/// This lookup will not work with Paratext, which does not yet use an .ldml file.
		/// </summary>
		/// <param name="language">ISO 639 language code.</param>
		/// <returns>Text direction (ltr or rtl), or ltr if not found.</returns>
		public static string GetTextDirection(string language)
		{
			string[] langCoun = language.Split('-');
			string direction = "ltr";
			try
			{
				if (AppDomain.CurrentDomain.FriendlyName.ToLower() == "paratext.exe" ||
					TextDirectionLanguageFile != null) // is paratext
				{
					string fileName = TextDirectionLanguageFile;
					if (fileName == null)
					{
						SettingsHelper settingsHelper = new SettingsHelper(Param.DatabaseName);
						fileName = settingsHelper.GetLanguageFilename();
					}
					foreach (string line in FileData.Get(fileName).Split(new[] { '\n' }))
					{
						if (line.StartsWith("RTL="))
						{
							direction = (line[4] == 'T') ? "rtl" : "ltr";
							break;
						}
					}
				}
				else
				{
					string wsPath;
					if (langCoun.Length < 2)
					{
						// try the language (no country code) (e.g, "en" for "en-US")
						wsPath = PathCombine(Common.GetLDMLPath(), langCoun[0] + ".ldml");
					}
					else
					{
						// try the whole language expression (e.g., "ggo-Telu-IN")
						wsPath = PathCombine(Common.GetLDMLPath(), language + ".ldml");
					}
					if (File.Exists(wsPath))
					{
						XmlDocument ldml = DeclareXMLDocument(false);
						ldml.Load(wsPath);
						var nsmgr = new XmlNamespaceManager(ldml.NameTable);
						nsmgr.AddNamespace("palaso", "urn://palaso.org/ldmlExtensions/v1");
						var node = ldml.SelectSingleNode("//orientation/@characters", nsmgr);
						if (node != null)
						{
							// get the text direction specified by the .ldml file
							direction = (node.Value.ToLower().Equals("right-to-left")) ? "rtl" : "ltr";
						}
					}
				}
			}
			catch
			{
				direction = "ltr";
			}
			return direction;
		}