public static void ReleaseMoFile(TMoFile mofile) { Debug.Assert(mofile != null); MoFilesCS.AcquireWriterLock(Timeout.Infinite); try { mofile.Users--; if (mofile.Users <= 0) { for (int i = MoFiles.Count - 1; i >= 0; i--) { if (MoFiles.Objects[i] == mofile) { MoFiles.RemoveAt(i); //Dispose()? break; } } } } finally { MoFilesCS.ReleaseWriterLock(); } }
private void CloseMoFile() { if (mofile != null) { TFileLocator.ReleaseMoFile(mofile); mofile = null; } OpenHasFailedBefore = false; }
public static TMoFile GetMoFile(string filename, TDebugLogger DebugLogger) { Int64 offset, size; // Find real filename offset = 0; size = 0; string realfilename = filename; if (filename.StartsWith(basedirectory)) { filename = filename.Substring(basedirectory.Length); int idx = filelist.IndexOf(filename); if (idx != -1) { TEmbeddedFileInfo fi = (TEmbeddedFileInfo)filelist.Objects[idx]; realfilename = TGnuGettextInstance.ExecutableFilename; offset = fi.offset; size = fi.size; #if DXGETTEXTDEBUG DebugLogger("Instead of " + filename + ", using " + realfilename + " from offset " + (offset).ToString() + ", size " + (size).ToString()); #endif } } #if DXGETTEXTDEBUG DebugLogger("Reading .mo data from file ''" + filename + "''"); #endif // Find TMoFile object TMoFile Result; MoFilesCS.AcquireWriterLock(Timeout.Infinite); try { string idxname = realfilename + " //\\ " + (offset).ToString(); int idx; if (MoFiles.Find(idxname, out idx)) { Result = (TMoFile)MoFiles.Objects[idx]; } else { Result = new TMoFile(realfilename, offset, size, TGnuGettextInstance.UseMemoryMappedFiles); MoFiles.AddObject(idxname, Result); } Result.Users++; } finally { MoFilesCS.ReleaseWriterLock(); } return(Result); }
private void OpenMoFile() { string filename; // Check if it is already open if (mofile != null) { return; } // Check if it has been attempted to open the file before if (OpenHasFailedBefore) { return; } if (SpecificFilename != "") { filename = SpecificFilename; #if DXGETTEXTDEBUG DebugLogger("Domain " + Domain + " is bound to specific file " + filename); #endif } else { filename = Directory + curlang + Path.DirectorySeparatorChar + "LC_MESSAGES" + Path.DirectorySeparatorChar + Domain + ".mo"; if (!TFileLocator.FileExists(filename) && !File.Exists(filename)) { #if DXGETTEXTDEBUG DebugLogger("Domain " + Domain + ": File does not exist, neither embedded or in file system: " + filename); #endif filename = Directory + curlang.Substring(0, 2) + Path.DirectorySeparatorChar + "LC_MESSAGES" + Path.DirectorySeparatorChar + Domain + ".mo"; #if DXGETTEXTDEBUG DebugLogger("Domain " + Domain + " will attempt to use this file: " + filename); #endif } else { #if DXGETTEXTDEBUG if (TFileLocator.FileExists(filename)) { DebugLogger("Domain " + Domain + " will attempt to use this embedded file: " + filename); } else { DebugLogger("Domain " + Domain + " will attempt to use this file that was found on the file system: " + filename); } #endif } } if (!TFileLocator.FileExists(filename) && !File.Exists(filename)) { #if DXGETTEXTDEBUG DebugLogger("Domain " + Domain + " failed to locate the file: " + filename); #endif OpenHasFailedBefore = true; return; } #if DXGETTEXTDEBUG DebugLogger("Domain " + Domain + " now accesses the file."); #endif mofile = TFileLocator.GetMoFile(filename, DebugLogger); #if DXGETTEXTDEBUG if (mofile.isSwappedArchitecture) { DebugLogger(".mo file is swapped (comes from another CPU architecture)"); } #endif // Check, that the contents of the file is utf-8 if (GetTranslationProperty("Content-Type").ToUpper().IndexOf("CHARSET=UTF-8") == -1) { CloseMoFile(); #if DXGETTEXTDEBUG DebugLogger("The translation for the language code " + curlang + " (in " + filename + ") does not have charset=utf-8 in its Content-Type. Translations are turned off."); #endif MessageBox.Show(null, "The translation for the language code " + curlang + " (in " + filename + ") does not have charset=utf-8 in its Content-Type. Translations are turned off.", "Localization problem", MessageBoxButtons.OK); Enabled = false; } }